# Solved: Batch File Code Explanation



## computerman29642 (Dec 4, 2007)

Could someone please explain to me exactly what the code below is doing?


```
@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"
```


----------



## devil_himself (Apr 7, 2007)

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"


```
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.
```


----------



## computerman29642 (Dec 4, 2007)

Can you break the "usebackq" and "tokens" portion of the code down a little more?


----------



## devil_himself (Apr 7, 2007)

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

```
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

```
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

```
b c
f g
j k
```
Now, why delims

lets say you have the follwoing contents in the text file

```
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


```
b c
f g
j k
```
more explanation on "usebackq " later ...


----------



## computerman29642 (Dec 4, 2007)

Thanks.  I really appreciate you taking the time to explain in more detail.


----------



## devil_himself (Apr 7, 2007)

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 (Dec 4, 2007)

I am starting to understand a little better. I am still a little confused, but your explanation has helped greatly. Thank you.


----------

