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:

        Its corresponding output is:

  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. The fall of the dungeon guardians - enhanced edition download free. Batch files, after executing a command, provides an error code called ERRORLEVEL. A non-zero value means an error has occured.

          • 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:

        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:

        The output of this would be:

        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:

Batch If Not Exist Multiple Files

The below program check even numbers and odd numbers. If a number is divided by 2, it means it is an even number. If the number is not divided by 2, then it is an odd number. @ECHO OFF set /a num=%1% SET /a mod=num%% 2 IF%mod% 0 ( ECHO%num% is even )ELSE ( ECHO%num% is odd ) How the above program works. If you do a lot of work in Windows batch files, the IF statement offers a very powerful way to add flexibility to your scripts. In this article you're going to learn about the five main types of IF statements you can use in a Windows batch file, how the correct syntax looks, and a realistic example for each.

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.

The batch script supports the conditional statements like if, if-else .etc. In this article, I will discuss how you can use if and else in the batch file.

You can see this article, Batch file commands

Batch file if statement

Bat File To Run Exe

The if the statement is one of the selection statements. It uses to select statements depending on the value of a controlling expression.

Batch files if not exist


In the above scenario, the statement will only be executed if the controlling expression is non-zero.

Let see an example,

The below script displays the message according to the argument enter by the user.

When User Enter 4:

When User Enter 6:

Batch if not defined

When User Enter anything except 4 and 6:

Batch file if else statement

if else is a selection statement that used to select statements depending on the value of a controlling expression.


If Noti Batch Files Online

In the above scenario, statement1 will only be executed if the expression is non-zero. if the expression is zero, then statement2 will be executed.

Let see an example,

The below program check even numbers and odd numbers. If a number is divided by 2, it means it is an even number. If the number is not divided by 2, then it is an odd number.

How the above program works:

1 Case:When you entered even number:

We know that the modular division of an even number by 2 is 0. So expression (num%% 2) return 0, as we know expression ( 0 0) return 1. Now controlling expression of if statement is non-zero then body associated with if statement will execute.

2 Case:When you entered an odd number:

Batch File If Not Exist

For odd number expression (num %% 2) return 1, so expression ( 1 0) return 0. Now controlling the expression of if statement is zero then body associated with if statement will skip.

Recommended Articles for you: