Wednesday, December 22, 2010

STOP SERVICES and FOR LOOPS in Batch files

www.tech-recipes.com


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
    1. no quotes around filename
    2. the "%%x" variables have two % symbols (this is because i'm running inside of a BAT already.
    3. there are just as many "tokens" as there are "%%x" variables
    4. there is only a space after the delims= declaration

lastly, the resulting output:NET STOP "Apache Tomcat 4.1.31"
NET STOP "Apache Tomcat 5.0.27"
NET STOP "MySQL41"

No comments:

Post a Comment