Rob van der Woude's Scripting Pages

Batch How To ...

Check your batch files with BatCodeCheck

NOTES: You may continue using this tool, as I still do myself, but I will no longer support 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.
Expect to see many "false positives", i.e. warnings that don't apply, especially "unterminated ..." type warnings.
My main rule while writing this tool was: I'd rather have 10 false positives than miss 1 real issue.

 

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.

Expect to see many "false positives", i.e. warnings that don't apply, especially "unterminated ..." type warnings.
It is impossible 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.

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.

 

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 have kept track of known issues in this list: Known Issues With BatCodeCheck.

Download BatCodeCheck

Download BatCodeCheck:
Version: 0.42 beta
File name: batcodecheck.zip
Last modified: 2025-04-14
Checksums:
MD5: 959d965b15b09ebb19b75d0de296702e
SHA1: 8b2658c4f2c7408939e0cf166bce7f5ae9dbcd63

page last modified: 2025-04-14; loaded in 0.0058 seconds