Some Random DOS Batch File Nuggets

It was mainly this last factor that was the driver behind my particular recent use, but it really is amazing what can be done with such scripts and there are a lot of great resources out there that show you how to do some pretty incredible things.

In this post I’m just going to highlight a few little tid-bits that helped me deliver the scripts I needed. There’s nothing earth shattering here, but perhaps one of these will be just what you’re looking for next time you need to resort to some good old batch file scripts.

Batch Script Exit Codes

The fundamental task at hand for me was to process a log file and based upon some analysis of the content to return an exit code to a task scheduler. Now whilst I can do this in a number of languages, the client’s preference was to have this done via a batch file as they were a known quantity and familiar to the staff who would be supporting it. So to return an exit code is a simple matter. You just use the EXIT command with the /B switch and specify the code you wish to return. Unfortunately first time around I misread the help and didn’t do things quite right.

As I had it, I expected the following code to return an exit code of 100 to the calling application.

EXIT 100 /B

The actual effect is that nothing was returned. After a while of hair pulling and not bothering to read the documentation (after all it was so simple wasn’t it?) I discovered that I had the order of the parameters for EXIT wrong. I needed to swap over the exit code and the switch. The command should have been …

EXIT /B 100

So the moral of the story is to check your syntax and grammar - even on those unbelievably simple statements!

I didn’t do the actual log analysis in a batch script, but instead in VBScript. I had an existing script that was relatively quick to adapt and again the client was comfortable with the solution. So for the sake of completeness I thought I’d include a tiny bit on how to return an exit code from VBScript.

Well the answer is a little bit of a cheat as I used WSH Script to do that piece. No switches this time just an instruction to terminate and the exit code you want to return. So the example below would return an exit code of 100.

WScript.Quit 100

Reference All Parameters in a Batch Script

To make things a little easier for the client’s scheduling folk I decided to put in a layer of abstraction. This involved some more batch scripts.

The script that analysed the log also had to call another script (there were a lot of scripts in this corner of the project) to generate the log in the first place. Now this script had several parameters it could run based on and these needed to be passed through from the scheduling program or from the batch file.

I created a set of logically named batch files with pre-set parameters that could be called by the scheduler. Each of these files called the same single master batch file that then triggered everything and returned the exit code (which then had to be returned by each of these scripts). By doing it this way it meant I could centralise the calling code in the master script. The trick was being able to process and pass through the parameters from these pre-set parameter scripts - irrespective of how many parameters were being passed through.

I’d used %1, %2, %3, … etc. to work with specific parameters before, but never all parameters and where they could vary in number. The answer as it turns out to just pass them all on was surprisingly simple.

%* can be used to represent the string of all parameters exactly as passed in. So here’s an example of how you might use it.

ECHO Script-A
ECHO One Parameter: Apple
ECHO Two Parameters: Apple Orange
CMD /K "SCRIPT-B.BAT Apple Orange"
ECHO Three Parameters: Apple Orange Pear
CMD /K "SCRIPT-B.BAT Apple Orange Pear"
ECHO Script-B
ECHO Script-C
ECHO Parameters = %*

This produces the following output:

One Parameter: Apple
Parameters = Apple
Two Parameters: Apple Orange
Parameters = Apple Orange
Three Parameters: Apple Orange Pear
Parameters = Apple Orange Pear
Press any key to continue . . .

Batch Scripting Time Stamps and Logs

As part of the scripts I also did a lot of logging. It makes things easier in the long run when you know exactly what the scripts were doing.

So first off I have a fondness for time stamps as it puts things in a good order for further processing; in case you need to sort of filter the data. So I created a function to output text to a log file and prefix it with the date and time.

For this particular logging I also wanted it to be in reverse chronological order. This meant that every time something new was logged it was added to the start of the file rather than the end. Now batch output to files isn’t that sophisticated and since I didn’t expect the log files to grow particularly huge (I can create a new one each day, week, run, etc.) I used a simple temporary file based solution.

The script below gives an idea about what I used.

REM Files used for logging
REM Log a couple of lines of information
CALL:write_to_log "Apple Orange Pear Bear"
CALL:write_to_log "Apple Pear Orange Bear"
ECHO Display log
ECHO -----------
REM Write timestamp, tab, log entry to a temporary file
ECHO.%DATE:/=-%-%TIME::=.%     %~1 > %TEMP_FILENAME%
REM If we have a log file append it to the end of the temporary file
REM If we have a log file delete it
REM Rename the temporary file to the log file


So there’s a handful of random DOS batch scripting nuggets. The particular piece of work yielded a few more interesting scripting solutions, but I’ll maybe cover them in a future post. Hopefully there’s been something of use to you here, even if it’s just to spark off an idea about something else.

Author: Stephen Millard
Tags: | shell |

Buy me a coffeeBuy me a coffee

Related posts that you may also like to read