<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>Tech-Recipes - Latest Comments in Processing the contents of a text file using FOR loop | Batch file programming | Tech-Recipes</title><link>http://tech-recipes.disqus.com/</link><description>Cookbook of Tech Tutorials</description><language>en</language><lastBuildDate>Thu, 11 Dec 2008 08:35:00 -0000</lastBuildDate><item><title>Re: Processing the contents of a text file using FOR loop | Batch file programming | Tech-Recipes</title><link>http://www.tech-recipes.com/rx/363/processing-the-contents-of-a-text-file-using-for-loop/#comment-4327099</link><description>Geza,&lt;br&gt;&lt;br&gt;Very simple&lt;br&gt;here is an example with your requirements&lt;br&gt;&lt;br&gt;For /f %%a in (test.txt) do echo computername = %%a&amp;gt;&amp;gt;result.txt &amp; copy \\server\file \\%%a\c$\folder &amp; net send admin %%a has been updated</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tom J</dc:creator><pubDate>Thu, 11 Dec 2008 08:35:00 -0000</pubDate></item><item><title>Re: Processing the contents of a text file using FOR loop | Batch file programming | Tech-Recipes</title><link>http://www.tech-recipes.com/rx/363/processing-the-contents-of-a-text-file-using-for-loop/#comment-2767385</link><description>Hello again,&lt;br&gt;&lt;br&gt;Does someone know how to run not one but two or more commands in a FOR loop?&lt;br&gt;&lt;br&gt;For example here is the example that I used at the begining of this discussion:&lt;br&gt;&lt;br&gt;For /f %%a in (test.txt) do echo computername = %%a&amp;gt;&amp;gt;result.txt &lt;br&gt;&lt;br&gt;where the single command to be executed is "do echo computername = %%a&amp;gt;&amp;gt;result.txt "&lt;br&gt;&lt;br&gt;But what is the syntax for this?&lt;br&gt;&lt;br&gt;1. grab the first/next computer name from the text file&lt;br&gt;2. echo computer name to another text file&lt;br&gt;3. copy a file from the server to a folder on the same computer&lt;br&gt;4. net send admin "computer" has been updated&lt;br&gt;&lt;br&gt;then go back and pick the next computer name from the text file and run the same commands again.&lt;br&gt;&lt;br&gt;So, the basic question is what is the syntax for running multiple commands with the same variable in a FOR loop?&lt;br&gt;&lt;br&gt;Geza</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Anonymous</dc:creator><pubDate>Sat, 07 Jul 2007 09:36:43 -0000</pubDate></item><item><title>Re: Processing the contents of a text file using FOR loop | Batch file programming | Tech-Recipes</title><link>http://www.tech-recipes.com/rx/363/processing-the-contents-of-a-text-file-using-for-loop/#comment-2767384</link><description>&lt;strong&gt;WINDOWS 2003&lt;em&gt;(did not test in XP)&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;The key is to use delimaters...well, sorta.  You want to trick the FOR loop into looking for delimiters.&lt;br&gt;&lt;strong&gt;~~ SERVICES.TXT ~~&lt;/strong&gt;&lt;br&gt;&lt;code&gt;; be sure to surround your values by QUOTES &amp;#40;&amp;quot;&amp;#41;&lt;br&gt;&amp;quot;Apache Tomcat 4.1.31&amp;quot;&lt;br&gt;&amp;quot;Apache Tomcat 5.0.27&amp;quot;&lt;br&gt;&amp;quot;MySQL41&amp;quot;&lt;br&gt;; and with the EOL switch, lines beginning in &amp;quot;;&amp;quot; are ignored&lt;br&gt;&lt;/code&gt;&lt;br&gt;&lt;strong&gt;~~ START.EM.UP.BAT ~~&lt;/strong&gt;&lt;br&gt;&lt;code&gt;FOR /F &amp;quot;eol=; tokens=1,2,3,4,5,6,7* delims= &amp;quot; %%a IN &amp;#40;SERVICES.TXT&amp;#41; DO NET STOP %%a %%b %%c %%d %%e %%f %%g&lt;br&gt;&lt;/code&gt;&lt;br&gt;also notice a few things&amp;lt;ul&amp;gt;1. no quotes around filename&amp;lt;/ul&amp;gt;&amp;lt;ul&amp;gt;2. the "%%x" variables have two % symbols (this is because i'm running inside of a BAT already.&amp;lt;/ul&amp;gt;&amp;lt;ul&amp;gt;3. there are just as many "tokens" as there are "%%x" variables&amp;lt;/ul&amp;gt;&amp;lt;ul&amp;gt;4. there is only a space after the &lt;em&gt;delims= &lt;/em&gt; declaration&amp;lt;/ul&amp;gt;&lt;br&gt;lastly, the resulting output:&lt;code&gt;NET STOP &amp;quot;Apache Tomcat 4.1.31&amp;quot;&lt;br&gt;NET STOP &amp;quot;Apache Tomcat 5.0.27&amp;quot;&lt;br&gt;NET STOP &amp;quot;MySQL41&amp;quot;&lt;/code&gt;&lt;br&gt;PERFECT !!!!!!!!!!!!!!!!!!!!!</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Anonymous</dc:creator><pubDate>Tue, 06 Jun 2006 10:14:07 -0000</pubDate></item><item><title>Re: Processing the contents of a text file using FOR loop | Batch file programming | Tech-Recipes</title><link>http://www.tech-recipes.com/rx/363/processing-the-contents-of-a-text-file-using-for-loop/#comment-2767383</link><description>i'm having the same difficulty.&lt;br&gt;&lt;br&gt;I'm trying to build a "START ALL THESE SERVICES" type batch file, and my Services.TXT file contains spaces.&lt;br&gt;&lt;br&gt;I just can't seem to get the BAT to ignore the spaces.  Has anyone else seen the same problem?&lt;br&gt;&lt;br&gt;&lt;em&gt;&lt;strong&gt;~~ SERVICES.TXT ~~&lt;/strong&gt;&lt;br&gt;Apache Tomcat 5.0.27&lt;br&gt;MySQL 41&lt;br&gt;&lt;br&gt;&lt;strong&gt;~~ STARTEM.UP.BAT ~~&lt;/strong&gt;&lt;br&gt;FOR /f %%a in (SERVICES.TXT) do NET START "%%a"&lt;/em&gt;&lt;br&gt;&lt;br&gt;all it tries to run is:&lt;br&gt;&lt;em&gt;NET START "Apache&lt;/em&gt;&lt;br&gt;&lt;br&gt;thanks for the GREAT tip though.&lt;br&gt;Michael "SPENGLER" Lowden</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Anonymous</dc:creator><pubDate>Tue, 06 Jun 2006 09:43:16 -0000</pubDate></item><item><title>Re: Processing the contents of a text file using FOR loop | Batch file programming | Tech-Recipes</title><link>http://www.tech-recipes.com/rx/363/processing-the-contents-of-a-text-file-using-for-loop/#comment-2767382</link><description>the last result is:&lt;br&gt;Dir "C:Documents&lt;br&gt;&lt;br&gt;not Dir "C:Documents_and</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Anonymous</dc:creator><pubDate>Mon, 03 Apr 2006 22:56:08 -0000</pubDate></item><item><title>Re: Processing the contents of a text file using FOR loop | Batch file programming | Tech-Recipes</title><link>http://www.tech-recipes.com/rx/363/processing-the-contents-of-a-text-file-using-for-loop/#comment-2767381</link><description>i tried your ide, and that's work fine, but....&lt;br&gt;&lt;br&gt;My source file contain:&lt;br&gt;C:Documents and Settingsyves&lt;br&gt;C:Documents and Settingsvalerie&lt;br&gt;etc..&lt;br&gt;&lt;br&gt;The batch just do a DIR (finally i would like to make a xcopy)&lt;br&gt;&lt;br&gt;The result is:&lt;br&gt;Dir C:Documents&lt;br&gt;&lt;br&gt;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:&lt;br&gt;Dir "C:Documents_and&lt;br&gt;&lt;br&gt;So, how to resolv that ??&lt;br&gt;Thanks, Yves</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Anonymous</dc:creator><pubDate>Mon, 03 Apr 2006 22:54:36 -0000</pubDate></item><item><title>Re: Processing the contents of a text file using FOR loop | Batch file programming | Tech-Recipes</title><link>http://www.tech-recipes.com/rx/363/processing-the-contents-of-a-text-file-using-for-loop/#comment-2767380</link><description>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:&lt;br&gt;&lt;br&gt;For /f %%a in (test.txt) do echo computername = %%a&amp;gt;&amp;gt;result.txt&lt;br&gt;&lt;br&gt;Geza</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Anonymous</dc:creator><pubDate>Wed, 18 May 2005 15:30:45 -0000</pubDate></item><item><title>Re: Processing the contents of a text file using FOR loop | Batch file programming | Tech-Recipes</title><link>http://www.tech-recipes.com/rx/363/processing-the-contents-of-a-text-file-using-for-loop/#comment-2767379</link><description>Hello,&lt;br&gt;&lt;br&gt;I tried the following line from a batch file, from command line with one and two "%"s, it just doesn't work.&lt;br&gt;&lt;br&gt;FOR /f %%a in ('test.txt') do echo Computer: %%a&lt;br&gt;&lt;br&gt;(I do have a test.txt file in the same folder that contains 5 server names one below the other, like this:&lt;br&gt;&lt;br&gt;s02&lt;br&gt;s03&lt;br&gt;s04&lt;br&gt;s05&lt;br&gt;s06&lt;br&gt;&lt;br&gt;The only thing it does, that it will actually open the test.txt file and that's it.&lt;br&gt;I also tried the script that someone else posted here as a comment, that didn't work either.&lt;br&gt;&lt;br&gt;Would some post a whole, complete but simple script for a batch file, that will do  &lt;br&gt;          echo %%a&amp;gt;&amp;gt;result.txt  ?&lt;br&gt;&lt;br&gt;Thanks,&lt;br&gt;Geza</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Anonymous</dc:creator><pubDate>Wed, 18 May 2005 15:21:53 -0000</pubDate></item><item><title>Re: Processing the contents of a text file using FOR loop | Batch file programming | Tech-Recipes</title><link>http://www.tech-recipes.com/rx/363/processing-the-contents-of-a-text-file-using-for-loop/#comment-2767378</link><description>Thanks for the tip on subroutines.  It really helped.</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">jkhax0r</dc:creator><pubDate>Tue, 23 Mar 2004 12:27:23 -0000</pubDate></item><item><title>Re: Processing the contents of a text file using FOR loop | Batch file programming | Tech-Recipes</title><link>http://www.tech-recipes.com/rx/363/processing-the-contents-of-a-text-file-using-for-loop/#comment-2767377</link><description>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:&lt;br&gt;&lt;br&gt;&lt;strong&gt;FOR /f %%a in ('complist.txt') do call :MY_SUB %%a&lt;br&gt;GOTO SUB_DONE&lt;/strong&gt;&lt;br&gt;&lt;strong&gt;:MY_SUB&lt;/strong&gt;&lt;br&gt;&lt;br&gt;REM Here, the computer name was passed as a parameter, so it comes in as %1&lt;br&gt;&lt;br&gt;REM Example - copy a file to the computer&lt;br&gt;copy myfile.txt \%1c$&lt;br&gt;&lt;br&gt;REM run this to end the subroutine&lt;br&gt;&lt;br&gt;&lt;strong&gt;GOTO :EOF&lt;/strong&gt;&lt;br&gt;&lt;strong&gt;:SUB_DONE&lt;/strong&gt;&lt;br&gt;&lt;br&gt;REM subroutine is finished, rest of the batch file continues</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">jwalker</dc:creator><pubDate>Tue, 24 Feb 2004 05:45:09 -0000</pubDate></item></channel></rss>