Community Page
- www.tech-recipes.com/ Jump to website »
-
Subscribe -
Community
-
Top Commenters
-
Popular Threads
-
Recent Comments
- Google is the best search engine...what else I can say? But I dont use google chrome, I prefer other browser
- Thank you so much i haven't had to do that on vista and nearly two years and have used a seperate quick launce bar for many many years and it was driving me insane
- Lovely! I completely forgot about dos2unix and sincerely I like to use vim better if I can :)
- hey shay, just added you. add me if you want 4285 2670 2528 4658
- Thats cool Stuff as i m Nebie to unix
Tech-Recipes
Cookbook of Tech TutorialsProcessing the contents of a text file using FOR loop | Batch file programming | Tech-Recipes
Started by qdideas · 9 months ago
5 years ago
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
5 years ago
4 years ago
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
4 years ago
For /f %%a in (test.txt) do echo computername = %%a>>result.txt
Geza
3 years ago
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
3 years ago
Dir "C:Documents
not Dir "C:Documents_and
3 years ago
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
3 years ago
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 !!!!!!!!!!!!!!!!!!!!!
1 year ago
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
6 months ago
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