-
Website
http://www.tech-recipes.com/ -
Original page
http://www.tech-recipes.com/rx/363/processing-the-contents-of-a-text-file-using-for-loop/ -
Subscribe
All Comments -
Community
-
Top Commenters
-
davak
83 comments · 1 points
-
Web Design
3 comments · 1 points
-
danishbacker
9 comments · 1 points
-
flexinfo
11 comments · 1 points
-
Tonychelle
4 comments · 1 points
-
-
Popular Threads
-
Facebook: How To Get Only Status Updates on Your FB Home Page
1 week ago · 4 comments
-
Firefox: Enable Case Sensitive Searches When Using Find (Ctrl+F)
4 days ago · 1 comment
-
Windows 7: How To Disable Live Preview for Taskbar Thumbnails
1 week ago · 2 comments
-
Gmail: How to block a sender from your inbox
2 weeks ago · 3 comments
-
Our first iPhone game GreenThumb available in the App Store
3 weeks ago · 2 comments
-
Facebook: How To Get Only Status Updates on Your FB Home Page
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
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
For /f %%a in (test.txt) do echo computername = %%a>>result.txt
Geza
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
Dir "C:Documents
not Dir "C:Documents_and
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
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 !!!!!!!!!!!!!!!!!!!!!
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
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