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.
95} How to perform an operation on all the filenames listed in a file?
Slightly different variants of this Frequently Asked Question have
been answered many times over. But let's look at this rendition.
Assume that you have in MyList.txt e.g. the following information.
The information could be there for example as a result of a search
with the DIR command.
C:\Documents and Settings\ts\Local Settings\Temp\GLB62.tmp
C:\Documents and Settings\ts\Local Settings\Temp\~DF3A.tmp
C:\WINDOWS\SET3.tmp
C:\WINDOWS\system32\CONFIG.TMP
C:\WINDOWS\Temp\scs17.tmp
C:\_L\TIMO\EXTENS01\EXTEN01.TMP
The script to build MyTask.cmd which you can then call separately.
@echo off & setlocal enableextensions
::
:: The command you want to perform on each row.
:: The "rem" has been inserted for safety
set todo_=rem del /p
::
:: Delete any old remnants
for %%f in ("MyTask.cmd") do if exist %%f del %%f
::
:: Add the todo_ command at the beginning of each line
:: Put use quotes in case the source file contains long file names
for /f "delims=" %%r in ('type MyList.txt') do (
>> "MyTask.cmd" echo %todo_% "%%~r")
::
:: Display the outcome also on the screen
type "MyTask.cmd"
echo.
dir "MyTask.cmd"|find /i "MyTask.cmd"
echo Is ready to be run
::
:: Clean up
for %%f in ("MyTask.cmd") do if exist %%f del %%f
endlocal & goto :EOF
The output will be
C:\_D\TEST>cmdfaq
rem del /p "C:\Documents and Settings\ts\Local Settings\Temp\GLB62.tmp"
rem del /p "C:\Documents and Settings\ts\Local Settings\Temp\~DF3A.tmp"
rem del /p "C:\WINDOWS\SET3.tmp"
rem del /p "C:\WINDOWS\system32\CONFIG.TMP"
rem del /p "C:\WINDOWS\Temp\scs17.tmp"
rem del /p "C:\_L\TIMO\EXTENS01\EXTEN01.TMP"
03.05.2005 12:35 317 MyTask.cmd
Is ready to be run
What about making the MyList.txt list. The origin could be anything,
but below is a familiar example how to list all today's *.TMP files
into it.
@echo off & setlocal enableextensions enabledelayedexpansion
::
:: Get today's date elements, locale dependent
set dd_=%date:~0,2%
set mm_=%date:~3,2%
set yyyy_=%date:~6,4%
::
:: List all *.TMP file made today
for /f "delims=" %%f in (
'dir "C:\*.tmp" /s /b /-c /a:-d-s-h') do (
set fdate_=%%~tf
set fdd_=!fdate_:~0,2!
set fmm_=!fdate_:~3,2!
set fyyyy_=!fdate_:~6,4!
if !fyyyy_!!fmm_!!fdd_! EQU %yyyy_%%mm_%%dd_% (
>> "MyList.txt" echo %%~f)
)
endlocal & goto :EOF