Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch Filename Date 2

Status
Not open for further replies.

trudye10

Programmer
Sep 8, 2006
67
I am not sure I am in the right forum but here goes. I am trying to get the syntax for adding a date to a filename in a Batch file. I know the command looks like:

for /f "tokens=1,2,3 delims=- " %%i in ('echo %DATE%') do (set BACKUP=%%k%%j%%i)

but that's gibbrish to me I a little slow, I need a syntax layout so I can tell what I'm coding in case I want to alter it.

Any help would be appreciated. IF this is the wrong forum could someone point me to the correct forum.

Thanx much,
Trudye
 
This is one of the right forums for this - you might be better posting in XP forum (forum779) as that gets more visitors, and batch commands much the same for XP and 2k. This sort of topic has been discussed a number of times before on both forums - might be worth searching either/both forums (been a while since I last saw such a post). I know a little about this - but not enough to advise as you are asking, but there are definitely forum members who do.
 
The easiest way to do this is to rename the file using the REN command and rename it with the %DATE%. It would be something like:

REN filename.txt filename%DATE%.txt

The problem with doing this is the %DATE% returns and address in the form of "Thu 06/07/2007". What I usually do is seperate out the parts that I need. You can grab just certain parts of the %DATE% by using :~ and then asking for particular pieces. For example if I wanted to just grab the 3 character day of the week, I would use %DATE:~0,3%. This says, "I want the %DATE%, but I only want the first 3 characters returned by %DATE%. If I wanted just the year, I would use %DATE:~10,4%. The first number is where to start, in this case count 10 places from the beginning of the returned %DATE% (10 spaces counts from before the "T" in "Thu" and after the "/") and then uses the next 4 places"

When I usually rename a file with the date, I want the month day and year all seperated by underscores. So for me to rename a file with that I use:

REN filename.txt filename%DATE:~4,2%_%DATE:~7,2%_%DATE:~10,4%

This renames my file "filename06_07_2007.txt".

Hope this helps. Please let me know if you have any questions.
 
I'm sorry I should have posted the fact that, I finally figured out what all that gobbledy-gook meant (I'm a little slow).

Thanx so much for responding.

While I have your attention are you aware of a batch command that allows you compress a file/dbase?

Thanx again,
Trudye
 
I am trying to write a batch file that will read data from a file and substring a portion of that data and write it to another file. The input file looks something like this:
00000000000004045
00000000000001021
I want to write 4045 and 1021 to another file. So far I have been able to do the following:
I can get the substring when SETing the value
SET _string=1234567890
SET _substring=%_string:~5,3%
echo %_substring%
-------------------------------------
Result is 678
I can read lines of data from a file
FOR /F %%i IN (c:\a\test.txt) DO @echo %%i
-------------------------------------
Result is 00000000000004045
00000000000001021
I am having trouble substringing the value I am reading
FOR /F %%i IN (c:\a\test.txt) DO (SET _substring=%%i:~8,3%, @echo %_substring%)
-------------------------------------
Result is:
(SET _substring=00000000000004045:~8,3_substring )
(SET _substring=00000000000001021:~8,3_substring )

What do I do to get the result? 4045
1021
Thanks so much,
Chris
 
Try this:

FOR /F %%i IN (c:\a\test.txt) DO SET string=%%i

SET substring=%STRING:~-4,4%

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top