FOR /F Explaination

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dringkoy
Posts: 2
Joined: 01 Jan 2014 20:53

FOR /F Explaination

#1 Post by dringkoy » 01 Jan 2014 21:12

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: FOR /F Explaination

#2 Post by foxidrive » 01 Jan 2014 21:25

Which parts do you have trouble with?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: FOR /F Explaination

#3 Post by Ed Dyreen » 01 Jan 2014 21:25


ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: FOR /F Explaination

#4 Post by ShadowThief » 01 Jan 2014 21:27

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

berserker
Posts: 95
Joined: 18 Dec 2013 00:51

Re: FOR /F Explaination

#5 Post by berserker » 02 Jan 2014 02:42

dringkoy wrote:I have no experience with batch scripts

what other scripting/programming experience do you have? And what tools are available to you?

dringkoy
Posts: 2
Joined: 01 Jan 2014 20:53

Re: FOR /F Explaination

#6 Post by dringkoy » 02 Jan 2014 10:12

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. :)

Post Reply