Batch files will seldom be perfect right away.
This page describes some debugging techniques that will help you find
and correct the errors.
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 OFF
line, i.e. REM @ECHO OFF or
:: @ECHO OFF
mybatch.bat any_optional_parameters > mybatch.logmybatch.bat any_optional_parameters > mybatch.log 2>&1LOGBATCH.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:
ECHO.MyVariable=%MyVariable%SET MyVariableMyVariable=MyValue
| 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 placed between parentheses) |
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
ECHO.=====================================================
TYPE %Temp%.\apipaorg.reg | FIND "[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\"| Note: | The ECHO command is inserted to mark where the
output of the first TYPE command ends and the next
one starts |
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 Counter=0
SET /A Counter += 1
SET
command; 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 cmdNT4.exe, cmdW2K.exe, cmdXP.exe, cmdXP2.exe etcetera.
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 cmdXXX.exe /K VER and compare the version and build numbers to those of the "host" OS' own VER command.
Looks familiar? |
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
| unique visitors since July 2007 | page last uploaded: 15 March 2010, 21:20 |