Solved: Batch File Code Explanation

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

computerman29642

Thread Starter
Joined
Dec 4, 2007
Messages
2,895
Could someone please explain to me exactly what the code below is doing?

Code:
@echo off
set filepath=C:\Test
For /f "usebackq tokens=*" %%I in (`dir /b /a-d "%filepath%weekending*.xls"`) do start /I excel.exe "%filepath%%%I"
 
Joined
Apr 7, 2007
Messages
4,910
open command prompt and type

help set
help for
help start
help dir


for information on that commands

::turns command echoing off
@echo off
::assign "C:\Test" to the variable "filepath" .
set filepath=C:\Test
::loop through all the "weekending*.xls" files in "C:\Test"(filepath) and start it using
::excel utility
For /f "usebackq tokens=*" %%I in (`dir /b /a-d "%filepath%weekending*.xls"`) do start /I excel.exe "%filepath%%%I"

Code:
 tokens=x,y,m-n  - specifies which tokens from each line are to
                   be passed to the for body for each iteration.
                   This will cause additional variable names to
                   be allocated.  The m-n form is a range,
                   specifying the mth through the nth tokens.  If
                   the last character in the tokens= string is an
                   asterisk, then an additional variable is
                   allocated and receives the remaining text on
                   the line after the last token parsed.
 usebackq        - specifies that the new semantics are in force,
                   where a back quoted string is executed as a
                   command and a single quoted string is a
                   literal string command and allows the use of
                   double quotes to quote file names in
                   filenameset.
 
Joined
Apr 7, 2007
Messages
4,910
delims (delimiters){default = space}: used to break the line up into chunks so that you can reference with variables %i, %j, %k, ...
tokens{default = 1}: used so you can tell the FOR command how many chunks you want to break it up into and how many will be handled by variables.

lets say you have a text file
file.txt , sapce delimited
Code:
a b c d
e f g h
i j k l
so what this code is upposed to do
for /f %i in (file.txt) do @echo %i -- echo's the first field of file.txt

the 'for loop' loops through all the lines of the file and assigns the first chunk to the variable %1 (space being the default delimiter)

output
Code:
a
e
i

let say you want the second and the third field of every line
for /f "tokens=2,3" %i in (file.txt) do @echo %i %j -- no need to specify delimiter space being the default

output
Code:
b c
f g
j k
Now, why delims

lets say you have the follwoing contents in the text file
Code:
a:b:c:d
e:f:g:h
i:j:k:l
now you want the second and the third field of every line
for /f "tokens=2,3 delims=:" %i in (file.txt) do @echo %i %j

now we need to indicate that ":" are to be used as a delimiter

output

Code:
b c
f g
j k

more explanation on "usebackq " later ... :)
 
Joined
Apr 7, 2007
Messages
4,910
usebackq has many uses . particularly it is used when you want "for" to parse strings that contain one or more double quotes

this will not work correctly
for /f "tokens=1-4" %a in ("a","b","c","d") do echo %a %b %c %d

this would
for /f "usebackq tokens=1-4" %a in ('"a","b","c","d"') do echo %a %b %c %d
 

computerman29642

Thread Starter
Joined
Dec 4, 2007
Messages
2,895
I am starting to understand a little better. I am still a little confused, but your explanation has helped greatly. Thank you. :)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top