Batch files are DOS command line commands batched together. In Linux they are known as shell scripts, and follow a completely different syntax. Early Windows users had to use a batch file (autoexec.bat) to allocate a drive letter to their CD-ROM's, in order to install Windows from CD. Batch files are not so crucial nowadays, although still supported by newer versions of Windows.

Under Windows XP/2000+, batch files (*.bat) run in a special window (aka Command Prompt) created by c:\window\system32\cmd.exe (this could be called command.com in some instances). Commands can be typed in individually, or listed sequentially in a batch file, requiring use of batch file language. This How-To will tell you how to create and run a Microsoft batch file, giving a simple backup as an example.

Steps

  1. 1
    Open your text editor. Expect to use keys A-Z/0-9/, the symbols (!$| etc), and Enter. Most commands do not check entry for upper/lowercase, so for the moment don't worry about CAPS (or cApS). Each command (and its parameters) goes on one line. Open a command line window (cmd.exe) to test the commands you want to run. Arrange your windows so you can see them both.
  2. 2
    Start writing the file. To start writing the file, most people start with @echo off as it stops each command being printed as it is processed. It reduces clutter on the user's screen.
      : @echo off
      
    Advertisement
  3. 3
    Hit Enter. Remember to press Enter after each command.
  4. 4
    Welcome the user to the program. Type:
      : echo Welcome to the Backup Script!
      
  5. 5
    Hit Enter again.
  6. 6
    Leave a blank line for neat spacing then continue typing yet another line.
      : echo.
      
  7. 7
    Press Enter one more time.
  8. 8
    Program what options you want the person who runs your program to see. This code below gives the user a choice. Either they press F, or N, or they press Q or CTRL-Z which cancels the whole script.
      : choice /C:FNQ /N Select [F]ull Backup or [N]ew files only. Press [Q] or [CTRL-Z] to exit.
      
  9. 9
    Create commands for each choice. If the user presses Q the program returns a "3", and goes to section "end". If they press N the program returns a "2", and goes to section "small_backup". If they press F, the program returns a "1", and goes to "full_backup". "Errorlevel" is not an error message as such, just the only way to set output from the CHOICE command.
      : IF errorlevel 3 goto end
      : IF errorlevel 2 goto small_backup
      : IF errorlevel 1 goto full_backup
      
  10. 10
    Create those sections referred to above. Type:
      : :small_backup<br>
      : echo.
      : echo.
      : echo You chose to backup NEW files. Hit any key to start or ctrl-z to cancel.
      pause >nul
      xcopy c:\mydirectory d:\mybackup /s/m/e
      goto end
      : :full_backup<br>
      : echo.
      : echo.
      : echo You chose to backup ALL files. Hit any key to start or ctrl-z to cancel.
      pause >nul
      xcopy c:\mydirectory d:\mybackup /s/e
      goto end
      : :end
      : exit
      
  11. 11
    Create the directories referred to above, and copy a few small test files into the source directory ready for testing. Later you can change those directory names to suit your real <my documents="">.
  12. 12
    Save the file in Notepad as "mybackup.bat".
  13. 13
    Double-click the file to run it.
  14. Advertisement
Method 1
Method 1 of 1:

Examining the Full Code

  1. 1
    Practice your copy and pasting skills on the following text.
         @echo off
      echo Welcome to the Backup Script!
      echo.
      choice /C:FN /N Select [F]ull Backup or [N]ew files Backup, or ctrl-z to exit.
      IF errorlevel 3 goto end 
      IF errorlevel 2 goto small_backup
      IF errorlevel 1 goto full_backup
      :small_backup
      echo.
      echo.
      echo You chose to backup NEW files. Hit any key to start or ctrl-z to exit.
      pause >nul
      xcopy c:\mydirectory d:\mybackup /s/m/e
      goto end
      :full_backup
      echo.
      echo.
      echo You chose to backup ALL files. Hit any key to start or ctrl-z to exit.
      pause >nul
       xcopy c:\mydirectory d:\mybackup /s/e
      goto end
      :end
      exit
      

Warnings

  • The CHOICE command is not included in Windows XP Home nor Professional and will cause the batch file to close abruptly without prior notice.
    ⧼thumbs_response⧽
  • While the commands shown here are pretty harmless, use of certain system commands in batch files are potentially dangerous or possibly deadly to your device if misused.
    ⧼thumbs_response⧽
Advertisement

Things You'll Need

  • A text editor such as Notepad, or a programmer's editor such as HTML-Kit. Programs that embed further information in files (like Word) are not suitable.
  • Access to a Command Prompt. Click <start><run>, and type "cmd". Or access the feature under <accessories> in the Start Menu.
  • Some files that you can do a test backup on. Try with a small directory with few files, until you get going.

About This Article

Tested by:
wikiHow Technology Team
wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 28 people, some anonymous, worked to edit and improve it over time. This article has been viewed 278,447 times.
How helpful is this?
Co-authors: 28
Updated: August 17, 2022
Views: 278,447
Categories: Programming
Advertisement