FOR /F "tokens=2,3,4,5,6 delims=/: " %%i in ("%date% %Time%") do @set d=%%k%%i
I have no experience with batch scripts, can someone please explain the above FOR /F line.
Thank you
FOR /F Explaination
Moderator: DosItHelp
Re: FOR /F Explaination
Which parts do you have trouble with?
-
- Expert
- Posts: 1167
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: FOR /F Explaination
dringkoy wrote:FOR /F "tokens=2,3,4,5,6 delims=/: " %%i in ("%date% %Time%") do @set d=%%k%%i
I have no experience with batch scripts, can someone please explain the above FOR /F line.
Thank you
/F is used for processing files and strings. In this case, it's the string that output by "%date% %Time%" (when I did it just now on an American PC it was "Wed 01/01/2014 22:15:31.76")
Delims=/: (note the space after the colon, because it's very important) splits up that string wherever there is a slash, colon, or space.
Tokens=2,3,4,5,6 takes those split up strings and assigns them to variables starting with %%i and continuing alphabetically (%%i is set to 01, %%j is set to 01, %%k is set to 2014, %%l is set to 22, %%m is set to 15, and %%n is set to 31.76)
%%i indicates what variable to start setting the delimited values at. This can be any letter, but it must be a single letter. Also, it is case-sensitive.
in ("%date% %time") says to use the output of the string in parentheses as the input for the for loop
do says that while there's data to process, the for loop will do what's after this part
@set d=%%k%%i sets the value of d to the combination of variables %%k and %%i without displaying anything on the command prompt (in the example above, %%k is 2014 and %%i is 01, so it is saying that d=201401
Re: FOR /F Explaination
dringkoy wrote:I have no experience with batch scripts
what other scripting/programming experience do you have? And what tools are available to you?
Re: FOR /F Explaination
Thanks ShadowThief for the detailed explanation. I'm a System Admin who know nothing about batch scripts
. I am looking for a script that I will include in my backup task using robocopy that will append the mm/dd/year on its log file. and it looks like the above line solved my problem. 

