1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Batch File Code Explanation

Discussion in 'DOS/Other' started by computerman29642, Oct 7, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. computerman29642

    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"
    
     
  2. devil_himself

    devil_himself

    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.
    
     
  3. computerman29642

    computerman29642 Thread Starter

    Joined:
    Dec 4, 2007
    Messages:
    2,895
    Can you break the "usebackq" and "tokens" portion of the code down a little more?
     
  4. devil_himself

    devil_himself

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

    computerman29642 Thread Starter

    Joined:
    Dec 4, 2007
    Messages:
    2,895
    Thanks. :) I really appreciate you taking the time to explain in more detail.
     
  6. devil_himself

    devil_himself

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

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

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/756903

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice