Howto:DOS batch file
From ByteWiki
What follows is a sample batch file I used to mark the SD2 assignments.
Contents |
basic DOS stuff
Since the assignment was pretty straight forward text processing, a simple batch file could run the thing and examine the output.
@echo off rem in pursuit of a beautiful batch file... REM uncomment the following to use older version of java. REM Note: this assumes the install path is correct. REM set PATH=C:\j2sdk1.4.2_01\bin;%PATH%
conditional logic
Here's an example of using conditional logic in a DOS file. If the marking directory already exists (eg, from a previous run of the test), use the rmdir command to remove it (deleting the contents in the proccess). The next line will then use the mkdir command to make a new directory.
@echo making marking directory... if exist marking rmdir /s/q marking mkdir marking @echo copying files to marking directory... copy .files*.* .marking copy .*.zip .marking cd marking
calling java commands from DOS
I love this next bit. If Java is installed, you can just call the jar command from DOS and it can extract the zip file. Then it's a simple process of compiling. I wouldn't normaly use the extra switches for compiling, but since this was for marking an assignment, I wanted to get feedback on deprecation and other non fatal issues.
@echo extracting zip file... jar -xf *.zip @echo compiling... javac -deprecation -Xlint:unchecked *.java
Then it's a simple proccess of running the java files with the appropriate input. Since this assignment has a predictable output, I just use the FC command to compare the programs output with the expected output.
@echo running first test... @echo on java Assign1 letter list outnormal @echo off fc outnormal.txt firstout.txt pause cls
Alan suggested adding the following line to help check if students had provided appropriate Javadoc style comments.
javadoc -private -d .doc *.java
Don't forget, a batch file can also take DOS_batch_file_parameters.
calling other batch files
You can use a batch file to launch another batch file just by invoking it by name. For example, it I had a batch file called FileA.bat
echo Hello
And another one called FileB.bat
echo World
Then I could try calling them both from FileC.bat like...
FileA FileB
Unfortunatly only the first one will be run. When the first batch file that is called completes, the file exits to DOS, but doesn't return to the calling file.
We need to use the CALL command. So FileC.bat should look like...
call FileA call FileB
This way, we wil get the output...
Hello
World
goto in DOS
Here's part of a batch file I used to mark an assignment where the students could submit either a zip file or source code.
if exist *.zip goto zip if exist *.java goto testing :zip jar -xf *.zip :testing REM testing code goes here...
Note that goto is not like a subroutine. You can fall out the bottom and into the next block of script. The goto statment find the named block (indicated with the ":" charecter) and starts executing from there.
The above code could be logicly neater, but I'm trying to demonstrate something ;)
Loops
The format of the FOR command is..
FOR variable IN list DO command [parameters] [switches]
You can loop over files for commands that don't support globbing. Just had to use this one at work:
for %%f in ("data*.ldif") do ldapmodify -h %ldap_host% -p %ldap_port% -D %ldap_usr% -w %ldap_pwd% -f "%%f" -r -c
Note that a double percentage ('%%') is required in batch files, but only a single one ('%') when used on the command line. This is a real pain.
Change your PATH variable from DOS
To set your PATH enviromental variable from teh DOS console, your can simply type
SET PATH=[path to set]
For example, to set your path to look in the current directory, you could type
SET PATH=.The gotcha here is that this will over ride anything already in your path, and there is probably stuff in there you don't want to mess with. To have a look at what's in your current path, type
ECHO %PATH%
If you don't want to damage what's already in your path, but wish to append something to the path, then type
SET PATH=%PATH%;[path to add]
For example, say you have installed Java to C:\apps\jdk1.5.0_06 (Not the default install directory, but that's what it is on the machine I'm using right now), then to be able to run java commands from dos, your could add it to your path by typing
SET PATH=%PATH%;C:appsjdk1.5.0_06bin
Note: This will not make a permanent change to your path. The new path we only last for the current dos terminal sesison. When you close the dos window, you will lose the changes and will have to reset your path again next tiem you start dos. To make a permanent change, you need to edit your Environmental Variables
Q: So why would you set the path this way? A: Because I don't have admin rights on my work machine so I can't access the Control Panel. This allows me to make temporary changes to the path without needing admin rights.
Note: when using Java, you might also need to pay attention to your classpath environmental variable.
Arguments
You can pass arguments into a batch file.
@echo off @echo %1 if exist %1 goto err mkdir %1 goto done :err @echo a folder with that name already exists. @echo action aborted. :done @echo finished
%1 refers to the first argument. Subsequent arguments would be numbered %2, %3... etc.
Categories: Howto | DOS
