DISQUS

Tech-Recipes: Processing the contents of a text file using FOR loop | Batch file programming | Tech-Recipes

  • jwalker · 5 years ago
    If you need to run multiple commands against one of the computers in the list, you can structure in the following way (my comments formatted for batch files; change double percents (%%) to single (%) to run on command line:

    FOR /f %%a in ('complist.txt') do call :MY_SUB %%a
    GOTO SUB_DONE

    :MY_SUB

    REM Here, the computer name was passed as a parameter, so it comes in as %1

    REM Example - copy a file to the computer
    copy myfile.txt \%1c$

    REM run this to end the subroutine

    GOTO :EOF
    :SUB_DONE

    REM subroutine is finished, rest of the batch file continues
  • jkhax0r · 5 years ago
    Thanks for the tip on subroutines. It really helped.
  • Anonymous · 4 years ago
    Hello,

    I tried the following line from a batch file, from command line with one and two "%"s, it just doesn't work.

    FOR /f %%a in ('test.txt') do echo Computer: %%a

    (I do have a test.txt file in the same folder that contains 5 server names one below the other, like this:

    s02
    s03
    s04
    s05
    s06

    The only thing it does, that it will actually open the test.txt file and that's it.
    I also tried the script that someone else posted here as a comment, that didn't work either.

    Would some post a whole, complete but simple script for a batch file, that will do
    echo %%a>>result.txt ?

    Thanks,
    Geza
  • Anonymous · 4 years ago
    Well, at least in Windows XP Pro, you do not need to put the filename between quotes ("test.txt") or ('test.txt') instead, you just have to use parenthesis like here:

    For /f %%a in (test.txt) do echo computername = %%a>>result.txt

    Geza
  • Anonymous · 3 years ago
    i tried your ide, and that's work fine, but....

    My source file contain:
    C:Documents and Settingsyves
    C:Documents and Settingsvalerie
    etc..

    The batch just do a DIR (finally i would like to make a xcopy)

    The result is:
    Dir C:Documents

    The space between the word Document and the word Settings stop the rest. I tried to use "C:Documents and Settingsyves" and the result was:
    Dir "C:Documents_and

    So, how to resolv that ??
    Thanks, Yves
  • Anonymous · 3 years ago
    the last result is:
    Dir "C:Documents

    not Dir "C:Documents_and
  • Anonymous · 3 years ago
    i'm having the same difficulty.

    I'm trying to build a "START ALL THESE SERVICES" type batch file, and my Services.TXT file contains spaces.

    I just can't seem to get the BAT to ignore the spaces. Has anyone else seen the same problem?

    ~~ SERVICES.TXT ~~
    Apache Tomcat 5.0.27
    MySQL 41

    ~~ STARTEM.UP.BAT ~~
    FOR /f %%a in (SERVICES.TXT) do NET START "%%a"


    all it tries to run is:
    NET START "Apache

    thanks for the GREAT tip though.
    Michael "SPENGLER" Lowden
  • Anonymous · 3 years ago
    WINDOWS 2003(did not test in XP)
    The key is to use delimaters...well, sorta. You want to trick the FOR loop into looking for delimiters.
    ~~ SERVICES.TXT ~~
    ; be sure to surround your values by QUOTES (")
    "Apache Tomcat 4.1.31"
    "Apache Tomcat 5.0.27"
    "MySQL41"
    ; and with the EOL switch, lines beginning in ";" are ignored

    ~~ START.EM.UP.BAT ~~
    FOR /F "eol=; tokens=1,2,3,4,5,6,7* delims= " %%a IN (SERVICES.TXT) DO NET STOP %%a %%b %%c %%d %%e %%f %%g

    also notice a few things<ul>1. no quotes around filename</ul><ul>2. the "%%x" variables have two % symbols (this is because i'm running inside of a BAT already.</ul><ul>3. there are just as many "tokens" as there are "%%x" variables</ul><ul>4. there is only a space after the delims= declaration</ul>
    lastly, the resulting output:NET STOP "Apache Tomcat 4.1.31"
    NET STOP "Apache Tomcat 5.0.27"
    NET STOP "MySQL41"

    PERFECT !!!!!!!!!!!!!!!!!!!!!
  • Anonymous · 2 years ago
    Hello again,

    Does someone know how to run not one but two or more commands in a FOR loop?

    For example here is the example that I used at the begining of this discussion:

    For /f %%a in (test.txt) do echo computername = %%a>>result.txt

    where the single command to be executed is "do echo computername = %%a>>result.txt "

    But what is the syntax for this?

    1. grab the first/next computer name from the text file
    2. echo computer name to another text file
    3. copy a file from the server to a folder on the same computer
    4. net send admin "computer" has been updated

    then go back and pick the next computer name from the text file and run the same commands again.

    So, the basic question is what is the syntax for running multiple commands with the same variable in a FOR loop?

    Geza
  • Tom J · 11 months ago
    Geza,

    Very simple
    here is an example with your requirements

    For /f %%a in (test.txt) do echo computername = %%a>>result.txt & copy \\server\file \\%%a\c$\folder & net send admin %%a has been updated