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 Command Filename Date 1

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
 
That's not always going to work. Your date format will have an effect.

tokens can be thought of as pieces of a complete string. In this case the complete string is the date that is echo'd by your computer.

delims are the delimiters that define how to break this sting into the pieces that you want. In the case that you've posted, the delimiters to break apart the date string are: the hyphen character and the space character. This is where the date format makes a difference. My date format uses slashes instead of dashes, so this won't work as expected on my computer.

%%i, %%j & %%k are varialbles that contain the first three tokens (or pieces) broken out of the complete string. There could be more or less, but in this case, you're asking for the first 3.

You're using those three variables to set another variable named BACKUP which would then presumably be used for some subsequent function like: ren %original_file% %BACKUP%

To illistrate & experiment use this:
Code:
@echo off
echo Today is %DATE%
for /f "tokens=1,2,3 delims=- " %%i in ('echo %DATE%') do (
echo k is %%k
echo j is %%j
echo i is %%i
)
pause
cls
You will find that the delimiters used must be consistant with the format of the echo %date% command, otherwise, you won't get the results that you expect.
 
Thank you so much smah for responding so quickly. I feel a little better about what I have to do. I'll experiment until it becomes as clear as mud (smile).

Be well,
Trudye
 
For clarity, I should have listed those variables in order & added more. Maybe something like this
Code:
@echo off
echo Today is %DATE%
for /f "tokens=1,2,3,4,5,6 delims=- /" %%i in ('echo %DATE%') do (
echo i is %%i
echo j is %%j
echo k is %%k
echo l is %%l
echo m is %%m
echo n is %%n
)
pause
cls
Change the tokens & delimiters to see what happens.
 
I put the code you suggested in a notepad txt file and ran it. What was I suppose to see? Nothing happened. I even tried turning echo on.

Trudye
 
If you save it as a batch file, it will tell you the date & then tell you each of the resultant variables.
 
Thanks for the info, I put the code in a .bat file and it did give me the current date. Can you provide me with a complete example of some batch code appending a date to a file?

Also as I said before I'm a little slow so I have some additional questions.

What I'm understanding from your code is; the date you want to echo has 6 parts, the date will be delimated by /'s. You have assigned each of those six parts to a alpha character (i.e., i thru n). What if you had used c thru h would it have mattered?

What does %%i mean?
Does in ('echo %DATE%') refer back to the second line of your code?
I assume the Do(...) is like an exec command (i.e. commence processing)

Thank you for all your help and patience,
Trudye
 
The [!]/?[/!] option of most shell commands will give you a wealth of information. For example, for /? at a command prompt will explain the for loop command in much more depth than I have given.

As for a complete example, there are plenty already in existence and there are a few ways to do it depending on what you ultimately need.

I arbitrarilly picked 6 tokens so that you could see the last one(s) will have no value [depending on date format]. If you split the date output into smaller parts using the delims mentioned, you will find that there are fewer than 6. And you've missed 2 delimiters that I wrote. There are 3 total in my code above: the hyphen charater (-), the space character ( ), and the forward slash character (/).

In general, from within the command shell %XXXXX% is how you'd call or use the variable XXXXX. This variable could be something previously set (see set /?), or it could be a system variable like date. One exception to this variable notation is within a for loop in a batch file - %%XXXXX notation is used. So, in this case, %%i is simply the variable i.

in ('echo %DATE%') defines what the for loop is processing. echo %DATE% is the command that generates some output to be evaluated. You can tell this is a command (as opposed to a string or file set) because of the single quote marks surrounding it (see for /?). The portion in the for loop is in no way related to the second line of the batch file. echo %DATE% stands alone as a command all by itself. The second line is only there for you (the human) to see the output of the variable %date% that gets evaluated inside the for loop; it is not required for processing. You can remove the second line of the batch file and you simply won't see the current date echo'd (see echo /?).

Yes, do is the processing that is to be done based on the results of the for loop. If it were only 1 command, it could be on the same line. Because this does several commands, I have put each command on a seperate line & enclosed them all in parenthesese.
 
Thank so much for taking the time to make that long explaination. Most of it I figured out earlier while I was waiting on your answer. I also managed to get the .bat job running.

However your explaination added some additional insight. With this new client I am sure I'll be writing other batch files and asking more "basic" questions.

Thanx again,
Trudye
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top