How to write a batch file: 10 steps (with pictures)

Table of contents:

How to write a batch file: 10 steps (with pictures)
How to write a batch file: 10 steps (with pictures)

In this article, you will learn how to write and save the simplest batch file (batch file) on a computer running Windows. A batch file contains a number of DOS (operating system) commands and is commonly used to automate frequently performed tasks such as moving files. To create a batch file, you don't need complex software - you only need the Notepad text editor.


Part 1 of 2: The Basics

4288 1 2
4288 1 2

Step 1. Open Notepad

In Notepad, you can write a program as a text file and then save it as a batch file. To open Notepad, open the Start menu


, enter notepad and click on the blue Notepad icon at the top of the menu.

Notepad is used to convert text files to batch files, but batch file code can be written in any text editor

  • Remember some basic commands. The batch file runs DOS commands, so the commands used are the same as DOS commands. Some of the most important commands are:
  • 4288 2 2
    4288 2 2
    • ECHO: displays text on the screen;
    • @ECHO OFF: hides the displayed text;
    • START: starts the file with the application;
    • REM: adds a line with comments;
    • MKDIR / RMDIR: creates and removes directories;
    • DEL: deletes files;
    • COPY: copies files;
    • XCOPY: copies files with additional parameters;
    • FOR / IN / DO: defines files;
    • TITLE: edits the title of the window.
  • Write a program to create a directory. The quickest way to learn how to create batch files is to start with the simplest tasks. For example, use a batch file to quickly create multiple directories:
  • 4288 3 2
    4288 3 2
    MKDIR c: \ catalog1 MKDIR c: \ catalog2

  • Write a program to create a backup. Batch files are great for running multiple commands at once, especially if those commands need to be run multiple times. Using the XCOPY command, you can create a batch file that will copy the files from the specified folders to the backup folder, and only those files that have changed since the last copy will be overwritten:
  • 4288 4 2
    4288 4 2
    @ECHO OFF XCOPY c: \ original c: \ backupfolder / m / e / y

    This program will copy the files from the "original" folder to the "backupfolder" folder. These folders can be replaced with others (with corresponding folder paths). / m indicates that only modified files will be copied; / e indicates that all subfolders (which are in the specified folder) will be copied; / y prompts you to overwrite the file

  • Write a more complex backup program. Simply copying files from one folder to another makes it easier to work with your computer, but what if you sort the files while copying them? This requires the FOR / IN / DO command. Use this command to specify which folder the file should be copied to, depending on its extension:
  • 4288 5 2
    4288 5 2
    @ECHO OFF cd c: \ source REM This is the source folder with the files FOR %% f IN (*.doc *.txt) DO XCOPY c: \ source \ "%% f" c: \ text / m / y REM All files with the extension.doc or.txt REM will be copied from c: \ source to c: \ text REM %% f this is the variable FOR %% f IN (*.jpg *.png *.bmp) DO XCOPY C: \ source \ " %% f "c: \ images / m / y REM All files with extension.jpg,.png,.bmp REM will be copied from c: \ source to c: \ images

  • Experiment with different commands. There are many examples of batch file programs on the Internet.
  • 4288 6 2
    4288 6 2
  • Part 2 of 2: How to Save a Batch File

    4288 7 2
    4288 7 2

    Step 1. Finish entering the batch file program

    When you are finished entering and editing the batch file program, save it as an executable file.

    4288 8 2
    4288 8 2

    Step 2. Click on File

    It's in the upper-left corner of the Notepad window. A dropdown menu will open.

    4288 9 2
    4288 9 2

    Step 3. Click Save As

    It's in the File drop-down menu. The "Save As" window will open.

    4288 10 2
    4288 10 2

    Step 4. Enter the name and extension.bat

    In the File Name box, enter a name, and then enter.bat.

    For example, if the batch file to be backed up will be named "backup", enter backup.bat

    4288 11 2
    4288 11 2

    Step 5. Open the dropdown menu "File Type"

    You will find it at the bottom of the Save As window.

    4288 12 2
    4288 12 2

    Step 6. Click on All Files

    It's in the drop-down menu. This will save the file with the specified extension (in this case, the.bat extension).

    4288 13 2
    4288 13 2

    Step 7. Specify the folder where the batch file will be stored

    To do this, click on the desired folder (for example, on "Desktop") on the left side of the window.

    4288 14 2
    4288 14 2

    Step 8. Click Save

    It's in the lower-right corner of the Save As window. The window will close.

    4288 15 2
    4288 15 2

    Step 9. Close Notepad

    The file will be saved as a batch file in the specified folder.

    4288 16 2
    4288 16 2

    Step 10. Modify the batch file program

    To do this, right-click on it and select "Change" from the menu. The batch file will open in Notepad; make the necessary changes and then save the file by pressing Ctrl + S.

    The changes will take effect as soon as you run the batch file


    • If the file or directory name contains spaces, enclose the name in quotation marks (for example, start "C: \ Documents and Settings \").
    • You can use third-party editors such as Notepad ++ to edit a batch file, but this is largely a waste of time if you are going to create basic batch files.
    • Some commands (for example, ipconfig) will require administrative privileges to run. In this case, right-click on the file and select "Run as administrator" from the menu (if you are logged in as administrator).

    Popular by topic