Processing the contents of a text file using FOR loop

Contributor Icon Contributed by gmartin Date Icon February 5, 2004  
Tag Icon Tagged: Batch file programming

As admins, we often have to take actions against a list of things (computers, servers, file shares, etc). One great way to approach this is to put the list in a test file and use a FOR command to loop through the file and take a action against the contents.


Say we have a file full of computernames:
complist.txt:
EricsPC
BobsPC
ExtraPC

and we need to delete each of these computers from the domain. Using a FOR loop to profress the file is the way to go (especially if there are really 300 computer names!)

First a test:
FOR /f %a in (’complist.txt’) do echo Computer: %a

should return
Computer: EricsPC
Computer: BobsPC
Computer: ExtraPC

To actually delete the PCs from the domain, change the command to:

FOR /f %a in (’complist.txt’) do net computer \\%a /DEL

When we run it we’ll see
net computer \\EricsPC /DEL
net computer \\BobsPC /DEL
net computer \\ExtraPC /DEL

Of course we can use this to run any command-line against any list. In fact, we can use the FOR to run a command that would generate the file.

(Note: This is valid command-line syntax. To run in a batch file, use two percent signs (e.g. ‘%%a’ )

\\Greg

Previous recipe | Next recipe |
 
  • jwalker
    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
    Thanks for the tip on subroutines. It really helped.
  • Anonymous
    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
    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
    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
    the last result is:
    Dir "C:Documents

    not Dir "C:Documents_and
  • Anonymous
    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
    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
    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
  • 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
blog comments powered by Disqus