Dos Batch Files
I wanted to show you a little known but very powerful DOS batch tool, called for.
for is a dos command that allows you to perform the same operation on a series of files. Here is the syntax:
for %f in (*.txt) do something %f
in a batch file, it would be %%f, for some reason. You can use %f, %g, %wibble anything except numbers and system variables.
Let's say you have a batch file called mover.bat that performs an operation on a file, like this that only moves a file if it doesn't already exist in a destination folder:
if not exist backup\%1 move %1 backup\%1
so mover somefile.txt will move the file somefile.txt to sub-directory backup if no such file is there already.
You can use the following statement to perform the operation on a list of files. For example if you wanted to only do this on text files (.txt) beginning with the letter w you would type the following:
for %f in (w*.txt) do call mover %f
The call is important, because when we talk to one batch file from another, execution will move to the new one, and without the call statement it will not return.
for also takes parameters you can use /D to operate on sub-directories, /R to look for files recursively. Type for /? at the command prompt to see all available options.
The following is an example batch file I use that uses for:
:* Program to find and unzip input files
:* Then run an executable to process them,
:* and zip up the output files.
echo Ensure CD is in Drive E:
if not exist unattended.yes pause
echo Deleting old data files...
if exist indats\*.dat del indats\*.dat
echo unpacking new dat files...
if exist tempzip.bat del tempzip.bat
for /R E:\ %%f in (*.zip) do unzip -j "%%f" -d .\indats\.
echo Commencing Main Program (mainProgram.exe)...
:* tempzip gets created by mainProgram as it runs.
:* At the end of the process it is called and zips ups the newly created files
echo Process Completed
See you in another couple of months probably....