Rob van der Woude's Scripting Pages

Batch How To ...

Check your batch files with BatCodeCheck

BatCodeCheck is a simple tool that checks your batch files for common errors and "bad practices".
It will display the batch files' source code, highlighting comments, ECHOed text and more, and adding warnings or error messages to lines that contain "dubious" or "suspect" code.
Below the highlighted code it will show a summary of its findings.
You should investigate the source code yourself, using BatCodeCheck's results for guidance only.
Because some errors are only reported once, rerun BatCodeCheck on the batch file after you investigated the warnings and corrected any errors.

BatCodeCheck was spawned from BatHL, which was intended to make batch files more "human readable" by highlighting the source code.
Development of BatHL has been terminated.

What BatCodeCheck can do

BatCodeCheck performs a series of regular expression based searches in a batch file's source code to detect syntax errors and logical errors.

BatCodeCheck looks for:

What BatCodeCheck cannot do

BatCodeCheck tests text files (i.e. batch files) for occurences of certain string patterns, it is not a debugger, nor an interpreter.
BatCodeCheck will not detect runtime errors, to detect these use the technique described in Debugging your batch files.

BatCodeCheck requires neatly written code.
I recommend reading the batch file best practices to get a general idea of what practices you need to avoid in order to put this tool to good use.
BatCodeCheck will most likely choke (i.e. generate false positives, or miss errors) on:

But hey, why would anyone want to use these obfuscating tricks anyway, as they make batch source code maintenace a real hell?

BatCodeCheck is intended to assist you in verifying your code, not to debug badly written or obfuscated code!
The more "advanced" the tricks in your code, the more "false positives" BatCodeCheck will generate and the more real issues will be overlooked.

"Debugging is always harder than programming, so if you write code as cleverly as you know how, by definition you will be unable to debug it."

The warnings BatCodeCheck generates are intended as guidance for you to "manually" investigate and correct if necessary.
BatCodeCheck cannot and will not make any corrections itself.

BatCodeCheck is a work in progress.
Expect to see many "false positives", i.e. warnings that don't apply, especially "unterminated ..." type warnings.
I am still working on these, but don't expect the tool to ever ever reach 0 false positives and 0 missed issues, except maybe on ECHO hello world like code.
When in doubt, I prefer BatCodeCheck generating an occasional false positive over missing a real issue.

How to use BatCodeCheck

Using BatCodeCheck is straightforward, open a command prompt and run it against your batch file:

BatCodeCheck.exe drive:\path\batchfile.bat /L /W

This command will display BatCodeCheck's findings in the command prompt, and write them to drive:\path\batchfile.log as well (plain text).
If you prefer HTML, use the optional /H switch to save the screen output in HTML format to drive:\path\batchfile.html (/L and /H may be used independently).
By default, existing log files (ASCII as well as HTML) will be overwritten. The optional /T switch will add a timestamp to the default log file names, which will prevent overwriting them.

The optional /W switch will wrap all warnings if their length exceeds the window width.
For really long batch files, this may not be a good idea, as more vertical scrolling will be required to read the output; running the program in a wider console window is recommended.

If the batch file uses environment variables that were set "outside" the batch file, you may want to add the optional /E switch (include local Environment variables).
This will prevent "Undefined variable" warning for those local variables.
Note however: those environment variables may be undefined on the computers running the batch file once it is deployed.

One-liners are not recommended, but if you do encounter them, the optional /S switch will split them into separate command lines and then perform its tests.

The new /I switch allows changing the settings Interactively for the current "run" only.

The interactive feature will only be available in the beta and licensed versions of BatCodeCheck.

Instead of typing the required command line switches at each run, you may save them in the environment variable %BatCodeCheck%, e.g. SET BatCodeCheck=/W/L/H/S/E.

The current version may still return many false positives — as stated before, BatCodeCheck is a work in progress, and still in beta.

 

Sample output:

 

After completing the test and correcting any real issues, rerun BatCodeCheck.
If there are no more issues left to be corrected, debug the batch file as described in the section Debugging your batch files

ToDo list

As stated before, BatCodeCheck is a work in progress.
The following features are planned (but not guaranteed) for future releases:

I keep track of known issues that I need to correct in this list: Known Issues With BatCodeCheck.
If you find any bugs/errors/unexpected results, please send the details to my e-mail address.

License

Unlike most of my scripts and software, commercial use of BatCodeCheck "final" versions (1.00 and up) will require a license!

No license is required for beta (0.*) versions, nor for personal use.

Download BatCodeCheck

Download BatCodeCheck:
Version: 0.32.1 beta
File name: batcodecheck.zip
Last modified: 2014-11-22
Checksums:
MD5: 7ac4094906c77b5765dba6ac3c96f1ef
SHA1: b02c31963b37d2069e342745abdda50d36eff633

page last uploaded: 2015-08-24, 21:09