Silicon Thumb

Since the 1980s, I've worked with computers, watched them grow, shrink, change and improve. I've worked with a lot of users and solved a lot of problems in that time too, so I thought this would be a good place to share some of the random things I've found and solved. If you have some odd problem, email me. If I can figure it out I'll post the answer here.

My Photo
Location: Mansfield, Texas, United States

I am a veteran computer geek, but I prefer the term 'Hired Gun', since that gives the (misleading) impression that I know what I'm talking about. I have worked on all sizes of system as an engineer, developer, technical support and operations, and at all levels from Operator to CIO.
I have some certifications, but what they are depends on what Microsoft is calling them this week.

If you have a question, and don't mind the answer being posted, email me here, removing the spam stopper.

Thursday, January 25, 2007

Dos Batch Files

Ok, I haven't written for ages, because has sucked away all my freetime. I went there to relax and end up spending most of my free time being paid to write code - go figure.

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:

@echo off
:* 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
call tempzip
echo Process Completed

See you in another couple of months probably....