Register / Log in
25
October

To ride on the heels of the previous post on redirecting standard input, batch files are another useful thing.

Batch files are a MS Windows deal. Unix systems have something similar called a shell script, but that’s for another day. A batch file, *.bat, is essentially a collection of commands that you’d otherwise type directly into a cmd window. There are a few other tricks that you can use to introduce variables, and thus make the batch files extremely useful for automation.

For example, one place in which I worked used batch scripts to copy files from one location to another. This copy process happened frequently, as we were deploying new code from development to testing machines. It would have been a pain to manually copy every file, digging down into each directory. Additionally, an added variable allowed us the power to specify the destination machine name.

To get started writing a batch script, open up your favorite text editor and enter the following text

dir
pause

Now execute the file, from wherever your saved it. The first line is a DOS command to display the contents of the current directory.  The second command is to pause, to wait for user input. When the batch script hits this line, it will say “press any key to continue”. Without this, the script will run and exit before you can even see what happened. Pauses are good for long and complicated scripts, to give the user a little feedback on what’s going on.

Let’s try something a little more involved.

:: @echo on means all commands will be echod to the console,
:: @echo off means that commands will not be echod to the console
@echo off

:: set the variables
:: %1 is the first argument passed in the command line
set myname=Athena
set yourname=%1
echo hello %yourname%, I am %myname%

:: %CD% is the current directory

echo Im currently working in %CD%, but m going to make a
echo directory called %CD%ExampleDir\
mkdir %CD%ExampleDir

echo Done, now Im going to move into that directory
cd %CD%ExampleDir
echo Now Im working in %CD%

echo Creating a file called deleteme.txt
echo please delete this file > deleteme.txt

echo renaming the folder to pleasedeleteme.txt
ren deleteme.txt pleasedeleteme.txt

echo copying pleasedeleteme.txt to copydeleteme.txt
copy pleasedeleteme.txt copydeleteme.txt

echo deleting all text files in this directory
del *.txt

echo backing out of this directory
cd ../

:: be careful with the del /s, /s will delete subdirectories
echo deleteting the directory, and any files in it,
pause
del /s .\ExampleDir

echo Done with everything!
pause

The above script does some fairly straightforward actions. Batch scripts, while sometimes simply, can get pretty intense. You can start getting loops and jumps and all kinds of crazy things. That is not to say that in order to write a useful batch script you must be a batch guru with all the knowledge in the book. Even the simplest batch scripts can save you tons of time!

View Comments

blog comments powered by Disqus