Skip to content

Batch Script Files

Learn how to create, save, execute, and edit batch files in Windows, including best practices, DO's and DON'Ts, and common commands like ECHO, PAUSE, and proper file naming.

Let's see how to create, save, execute, and edit batch files. In addition, DOs and DON'Ts suggestions are given.

Creating and Saving Batch Files

The easiest way to create a batch file is to use Notepad, but you can use any other text editor.

For example, let's try to create a basic batch file following these steps using Windows Notepad:

  1. Open Start.

  2. Search for Notepad and click the top result to open the text editor.

  3. Type the following lines in the text file to create a batch file:

    @ECHO OFF
    ECHO Hello World!
    PAUSE

    The above script outputs the phrase, "Hello World! " on the screen.

    • @ECHO OFF Shows the message on a clean line disabling the display prompt. Usually, this line goes at the beginning of the file. (You can use the command without the @ symbol, but it's recommended to include it to show a cleaner return.)
    • ECHO prints the text after the space on the screen.
    • PAUSE allows the window to stay open after the command has been executed. Otherwise, the window will close automatically as soon as the script finishes executing.
  4. Click the File menu, select the Save as option and confirm a name for the script (for example "first-script.bat")

Example of batch script in Notepad

Executing Batch Files

Once the batch file is created, simply double-click to run it.

Example of batch script execution

Modifying Batch Files

The easiest way to edit a batch file is to use Notepad, but you can use any other text editor.

Simply open the batch file with Notepad (or any other text editor), make the changes, and save.

Best Practice Suggestions

DOs

  • Documenting code with comments (see Batch Script Comments Chapter to learn more)
  • Validating input
  • Check variables before using them being sure that they are initialized.
  • Indentation

DON'Ts

  • Avoid one-line code (i.e. multiple command lines joined into a single line by ANDs and ORs) and use a block of code.
  • Avoid nested block codes (nested if-else) and use subroutines instead.
  • Don't use variable names as command names
Navigation