This page is edited from the 1cmdfaq.txt faq-file contained in
my tscmd.zip
command line interface (CLI) collection. That zipped file has much
additional material, including a number of detached .cmd script
files. It is recommended that you also get the zipped version as a
companion.
Please see "
The Description and
the Index page" for the conditions of usage and other such
information.
63} How to perform a command on each line of a file as an argument?
Q: I have an argument file with many lines. I want to perform the
same command with the contents of each line in turn as an argument.
How do I do that.
A: Say that you have the following argument file myargs.txt
row 1
row 2
row 3
row 4
And that you wish to echo (or whatever else) for the argument on
each line in turn.
@echo off & setlocal enableextensions
:: The argument file
set file_=myargs.txt
if not exist "%file_%" (
echo File %file_% not found
goto :EOF)
::
:: Make a temporary folder
if not exist c:\mytemp mkdir c:\mytemp
echo @echo off>c:\mytemp\doit.cmd
::
:: Whatever command you wish to perform
:: The example echoes, that is the second ECHO.
:: That ECHO command is what you would change to whatever
for /f "delims=" %%r in ('type "%file_%"') do (
echo ECHO.%%r>>c:\mytemp\doit.cmd)
::
:: Perform
call c:\mytemp\doit.cmd
::
:: Clean up
if exist c:\mytemp\doit.cmd del c:\mytemp\doit.cmd
rmdir c:\mytemp
::
endlocal & goto :EOF
The contents of your generated, temporary command script file doit.cmd
will have been
@echo off
ECHO.row 1
ECHO.row 2
ECHO.row 3
ECHO.row 4
which, when called, results in
row 1
row 2
row 3
row 4
The same method applies wheter the action you wish to perform on each
argument line is ECHO or something else.
With
SED the solution to make
c:\mytemp\doit.cmd would be
@echo off & setlocal enableextensions
:: The argument file
set file_=myargs.txt
if not exist "%file_%" (
echo File %file_% not found
goto :EOF)
::
:: Make a temporary folder
if not exist c:\mytemp mkdir c:\mytemp
echo @echo off>c:\mytemp\doit.cmd
::
:: Whatever command you wish to perform
:: The example echoes, that is the second ECHO.
:: That ECHO command is what you would change to whatever
sed -e "s/^/ECHO.&/" myargs.txt>>c:\mytemp\doit.cmd
::
:: Perform
call c:\mytemp\doit.cmd
::
:: Clean up
if exist c:\mytemp\doit.cmd del c:\mytemp\doit.cmd
rmdir c:\mytemp
::
endlocal & goto :EOF
A similar question from alt.msdos.batch.nt
Let's say I have a simple text file that is e.g. 8 lines long, and
each line contains exactly one number. (Each number can be 1 or 2
digits long.) For example:
2
5
12
1
15
6
7
2
How do I script my program to take the 8 numbers in the text file,
then output them to a file that contains exactly one line? Also, I
need each number to be semicolon delimited so that the output looks
like this: 2;5;12;1;15;6;7;2
This is the solution:
@echo off & setlocal enableextensions enabledelayedexpansion
for /f %%a in ('type "MyFile.txt"') do set s_=!s_!%%a;
set s_=%s_:~0,-1%
echo %s_%
endlocal & goto :EOF
The output will be exactly as asked for.
Actually
for /f %%a in ('type "MyFile.txt"') do set s_=!s_!%%a;
can be replaced by
for /f %%a in ("MyFile.txt") do set s_=!s_!%%a;