Windows Batch File Tips and Tricks

PushMon - Windows Batch File Tips and Tricks

Batch files have been around since the early Windows operating system. These are plain text files with .bat|.cmd|.btm file extensions, and when executed, the commands are interpreted by the Windows command-line interface.

While Microsoft released a more advanced command-line tool called PowerShell (this is not installed by default), the native windows command line remains a popular choice for scripting. Here are some tips and tricks for developers and non-technical people alike:

  1. Displaying of Comments or Remarks (ECHO and REM)

    Documentation is important, especially for very long batch files. However, during execution, these could clutter the display and can be difficult to read.

      • Using ‘@REM’ and ‘@ECHO OFF There are two differences between ECHO and REM. REM can totally be hidden during execution, while ECHO can at some level be suppressed, but will always be displayed during execution. Here are some tricks on how to change displays of comments and remarks by prepending with ‘@’ character:
          • @REM will hide the remark entirely and will not be shown on the command-line. All REM lines after an @ECHO OFF call will also be hidden. These are suitable for documentation intended for developers writing the script.
          • ECHO by default will be displayed during command invocation, as well as on the standard output. Using @ECHO or if preceded by @ECHO OFF will display the command on the standard output only. This is suitable for creating markers during execution to show at which point the command has executed so far.
      • Consider the following batch file:
        ECHO "ECHO displayed on the command line and on the standard output."
        @ECHO "ECHO is not displayed on the command line but will be displayed on standard output."
        REM "REM displayed on the command line."
        @REM "REM hidden from the command line."
        @ECHO OFF
        ECHO "all ECHO after @ECHO OFF will be shown on the standard output, but not on the command line invocation."
        REM "REM hidden from the command line after @ECHO OFF"
        

        Its corresponding output is:

        Batch File Tips and Tricks

        @REM and @ECHO tips.

  2. Error Handling

    In its simplest form, batch files are straightforward in its sequential command execution. However, there are tasks that need error handling, either for notification or cleanup. Batch files, after executing a command, provides an error code called ERRORLEVEL. A non-zero value means an error has occured.

      • ERRORLEVEL vs %ERRORLEVEL%
          • Both pertain to the current error code, and has the same value. The main difference between the two is that %ERRORLEVEL% is handled just like a variable, while ERRORLEVEL is handled specifically by the if operation. So if you would like to check for errors:
        if ERRORLEVEL 1 goto :ERROR
        if %ERRORLEVEL% neq 0 goto :ERROR

        The abovementioned snippets would have the same behavior. ERRORLEVEL checks if the value is equal to or greater than the number specified.

      • Example error handling in Windows batch files:
        @ECHO OFF
        
        ECHO "Force Error"
        ping -invalid-arg
        if ERRORLEVEL 1 goto ERROR
        REM /B to avoid the command line window to close
        EXIT /B %errorlevel%
        
        :ERROR
        ECHO "An ERROR has occurred."
        ECHO "Proceed with error handling or cleanup."
        

        The output of this would be:

        PushMon - Batch File Tips and Tricks

        Windows Batch File Error Handling Example

  3. Use VERIFY as a simple check for copying or moving files.

    Do you copy files within your batch file? It is worth it to add a one liner command VERIFY ON, to perform a simple check of the destination file. The operation is not comprehensive, as it does not check if the file is corrupted. However, if you are transferring a lot of files (e.g. backups, migration), then this is a first step to ensure integrity of the operation.

      • You can use xcopy or copy as copy operations:
    @ECHO OFF
    
    REM copy command with /v option; verify is a simple read operation
    copy /v PushMon.bat copy_PushMon.bat
    REM xcopy command /v option; a more advanced version which checks for the file size of the copied files
    xcopy /v PushMon.bat xcopy_PushMon.bat
    
    VERIFY ON
    REM copy command; verify is a simple read operation
    copy PushMon.bat copy2_PushMon.bat
    REM xcopy command; a more advanced version which checks for the file size of the copied files
    copy /v PushMon.bat xcopy2_PushMon.bat
    
    if ERRORLEVEL 1 goto ERROR
    EXIT %errorlevel%
    
    :ERROR
    ECHO "Copying of files have failed."
    

Do you have other tips and tricks for creating Windows Batch Files? Comment on this page and will add it up to the list.

PushMon supports the use of Windows batch files. If you have batch files that are scheduled to execute at specific intervals, you can use PushMon to monitor its execution and notify you if something goes wrong. We have created a separate blog post on how to monitor your batch files using Windows task scheduler.