Batch files will seldom be perfect right away.
This page describes some debugging techniques that will help you find and correct the issues.
Often running a batch file will result in a cryptic error message between (or sometimes instead of) the expected output.
To discover the source of the message, follow these steps:
@ECHO OFFline, i.e.
REM @ECHO OFFor
:: @ECHO OFF
mybatch.bat any_optional_parameters > mybatch.log
mybatch.bat any_optional_parameters > mybatch.log 2>&1
LOGBATCH.BAT mybatch.bat any_optional_parameters
A common source of errors are empty environment variables, or other unexpected values.
To check these values, follow these steps:
|Note:||Make sure delayed variable expansion is enabled if variables are set inside FOR loops or code blocks (a code block consists of multiple commands, either placed between parentheses or "joined" into a single line with ampersands)|
Another common source of errors are incorrectly redirected commands, like for example "nested" FIND or FINDSTR commands with incorrect search strings, sometimes within a FOR /F loop.
Here's a real-life example of such a one-liner:
FOR /F "tokens=8 delims=\" %%A IN ('TYPE %Temp%.\apipaorg.reg ^| FIND "[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\"') DO CALL :Enum "%%A"
To check the validity of these complex commands, follow these steps:
TYPE %Temp%.\apipaorg.reg | FIND "[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\"
Subroutines generating error messages pose an extra "challenge" in finding the cause of the error, as they may be called multiple times in the same batch file.
To help find out what causes the incorrect call to the subroutine, follow these steps:
SET /A Counter += 1
SETcommand; this will list all environment variables and their values.
If you intend to distribute your batch files to other computers that may or may not run the same Windows version, you will need to test your batch files in as many Windows versions as possible.
Some, but not all, known compatibility issues are:
The solution for compatibility issues with REG queries only is to issue the command twice, once for each version.
This technique is demonstrated in my iDate sample.
For testing of internal commands, I keep copies of
CMD.EXE of all my previous Windows versions.
For ease of use I renamed them
Now if I want to know if an internal command will work in NT 4 I use a command like this:
cmdNT4.exe /K DIR /?
|Note:||Use this trick carefully, the results won't always be identical to the results when run in the full OS.
To demonstrate this, run
But before distribution of your scripts you still need to run a full test on every Windows version the script is intended for.
If in doubt, you can control in which version(s) the batch file is allowed to run:
@ECHO OFF :: Check for Windows NT 4 and later IF NOT "%OS%"=="Windows_NT" GOTO DontRun :: Check for Windows NT 4 VER | FIND "Windows NT" >NUL && GOTO DontRun :: Check for Windows 2000 VER | FIND "Windows 2000" >NUL && GOTO DontRun :: Place actual code here . . . • • • :: End of actual code . . . EXIT :DontRun ECHO Sorry, this batch file was written for Windows XP and later versions only
page last uploaded: 2019-02-21, 09:59