Using Batch files

J

js_dev

If you are good at DOS commands but not yet familiar with the FOR
command, you can Skip this entire introduction to go to loops directly.


Batch prorgamming is an essential skill for any programmer, more so for
a Java programmer because, in the absence of an IDE he has to use the
standard command-line tools that ship with the JDK from Sun. In such a
situation, using the javac and java commands repetitively can be quite
irritating. Being able to write a script that does all the compling,
copying and deleting is a very useful skill. Another thing to learn is
Ant, which a compile tool which can be given a series of commands (as
many as you like) with lots of specifications to do all imaginable
tasks related with java. However, learning Ant is slightly more time
consuming (although much more rewarding for other tasks than just
compiling, running and moving files around) than batch scripts which
are extremely easy to learn.

First, make a list of all the commands in your command prompt (in Win
9x, Dos is much less powerful than cmd.exe of NT,2000 and XP):

c:\>help

for each of the listed commands, do the following

c:\>commandname /? >> commandname.help

so that you get the syntax into a file named commandname.help which you
can refer to even outside the DOS window.

e.g.

c:\>help

c:\>xcopy /? >> xcopy.help

then open this help file using Notepad or any other editor:

c:\>c:\windows\notepad.exe xcopy.help

This will bring up a Notepad window (a win32 GUI program). Come to
think of it, this shows the power of batch scripting - from a text file
named .bat you can invoke as many windows prorgams as you like! We'll
come to bat files in a moment.

Now that the xcopy.help file is open in another window, you can see the
syntax of the xcopy command. Note here that this help was made about 10
years back in the case of win 9x (when dos6.22 was released) and about
6 years back in case of win NT,2000,XP. You should therefore be able to
read the xcopy command help and make out what option does what to a
fair degree of understanding. One thing to keep in mind about batch
scripting - barring a few commands, you should learn to (with adequate
precaution) experiment with DOS commands. The commands to be careful
about are: format(erases your entire disk), deltree(deletes all data if
told to do so), del(occasionally can lead to deletion of important
files) and some others(protective clause :) ) . For experimenting with
copy, move, find, xcopy etc., I suggest that you make a set of folders,
if possible, on an unused partition, say, D:, E:, F: etc. and try out
all the commands there. Also, get into the habit of invoking other
programs from dos or from bat files - it is a very handy skill.

Now to batch files(.bat):

type the following lines into a new empty file in any editor (notepad
in windows , edit in dos ) and save the file as dir.bat
dir *.exe
dir c:\*.*
dir /ad /s c:\
pause

then go to the directory where you have saved this file in either dos
or windows explorer and run it by typing dir.bat or double-clicking.

The screen will scroll quickly showing a lot of directory listings and
then finally stop with "press any key to continue....". This gives you
an indication that if instead of the dir command you gave something
like

md d:\otherdir
xcopy c:\mydir\*test*.java d:\otherdir
d:
cd d:\otherdir
md bin
javac -d d:\otherdir\bin *.java

in your .bat file.

It says, first make a directory under d:\ named otherdir
then copy all java source files from c:\mydir\ containing "test" in the
name to d:\otherdir
then make a bin sub directory under otherdir
then use the javac command to compile all these files and put the class
files generated into the bin subdirectory

It would be very tedious if you had to do this entire procedure a few
times from the Dos window (agreed there is doskey, yet, individual
commands is very irritating) or from Windows Explorer, for that matter.
So , you see, batch files save a lot of work and also shield you from
potential mistakes - because you do not have to type in the commands
again and again, the chance of a typing error is reduced. Of course,
you must not make an error in the .bat file - but that is easy once you
have spent a week with .bat files.


Looping with FOR
Type the following
c:\>for /? >>for.help

c:\>c:\windows\notepad.exe c:\for.help

Now, in the notepad window, read the help and see if it makes sense. If
yes, good, continue and experiment, if no, try out the following:

make a file named myfor.bat with the following text inside it:

for %%I in (*.txt) do type %%I
pause

now run myfor.bat

this will cause each .txt file to be printed out.

% tells that the following letter is a variable

%% is required if the FOR command is given from inside a .bat file as
is the case here.
if you were to give the command at the command prompt you need not have
to use %%I, %I would do

c:\>for %I in (*.txt) do type %I

the command says:
for each I such that I is a file with any name but .txt extension, do
the following operation: type the contents of I.

You could as well have given any other operation:

for %%I in (*.help) echo %%I

which would echo the names of all .help files

For can be used recursively within an entire folder tree:

for /R c:\start\path\to\look\under %%I in (*.abc) do echo %%I

you can do a lot more than just echoing, but be careful, you are
dealing with the entire folder tree, if you make a mistake, you can
land into problems.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top