Create a Windows Batch File

A batch file (in DOS, OS/2, and Microsoft Windows) is a text file containing a series of commands intended to be executed by the command interpreter of the computer system. When a batch file is run, the shell program reads the file's instructions from top to bottom and performs any commands the batch file contains, normally line-by-line. Batch files are useful for running a sequence of executables automatically and are often used by system administrators to automate tedious processes.

Be Careful!

Batch files are very easy to create, but they can be extremely powerful.

How To Create a Batch File (on a Windows computer)

  • Create a simple text file in almost any text editor.
    (Note: Don't use Word, WordPad or any "word processor" Word uses propriety code when it creates characters. It looks like plain text, but it isn't.)
  • Type, or copy and paste your code (i.e. instructions) into the text file.
  • Save the file (in a special folder you create, if possible) with whatever name you want and a .bat extension.
    (Try creating one and name it "test.bat" (without the quotation marks) if you want a very easy one to try.)
  • To RUN a batch file: Double-click the file (in Windows Explorer)
    Or, create an icon on your Windows Desktop (New > Shortcut > Browse to wherever you saved the .bat file > Select the name of the .bat file > Click OK).
    Double-click the icon whenever you want it to run.
    You can select whatever icon you wish by right-clicking the icon, then clicking Properties > Change Icon > Click OK to "Choose icon from list or specify a different file" > Select an icon and Click OK.

Some Common Batch File Commands

  • call
    Can be used to call another batch file from within the current one
    call c:\batchfile2.bat
  • echo
    Used to display information and commands on the screen or prevent them from being displayed.
  • echo on
    causes all commands in the batch file to be displayed onscreen. This is the default setting.
  • @echo off
    causes no commands to be displayed. The batch file will run silently unless you use the echo command specifically as below.
  • echo what you want on the screen
    Causes whatever text comes after the echo command on the same line to be printed on the screen.
  • goto
    Moves to different points within a batch file. The destination point must be indicated with a colon. For example:
    goto end
    echo this is the end, beautiful friend… the end
  • if
    Performs a command depending on a condition. IF must include an ELSE statement which says what happens if the condition is not met. For example (command should be all be typed on one line):
if exist c:\myfile.txt (copy c:\myfile.txt d:\myfiles) else echo myfile.txt does not exist
    In the example above, if the 'myfile.txt' file exists, it will be copied to d:\myfiles. If it does not, a message will be shown indicating this.
    More info here about the IF command.

  • REM or rem
    Rem is used to create comments, or ignored sections in batch files. The computer will ignore any line that begins with REM, so you can use this command to add notations explaining what your file or line of code does. For example:
    @echo off
    rem this batch file is
    rem designed to format
    rem floppy disks in the a: drive
    format a:

    Note: You cannot use a redirection character "(" or ")" or pipe (|) in a batch file comment.
  • xcopy
    Copies a file or files (including directories and subdirectories) from one location to another.
    If the destination file(s) already exist, it/they will be overwritten and the contents replaced.

Some examples:

Example below: Filename: test.bat
This prints "Hello... This is a test batch file" and "Hello, again!" to your DOS screen.
@echo off
echo Hello... this is a test batch file
echo Hello, again!
Example below: Filename: whatever-you-want.bat
This creates a list of all the files (the names of the files) in your C:\Program Files folder and then creates a new text file on your C:\ drive (not in a sub-folder) and names the new file "list_of_program_files.txt". After inserting the list of filenames, the file is saved. You can just double-click on the "list_of_program_files.txt" in Windows Explorer to view the contents.
@echo off
dir "C:\Program Files" > C:\list_of_program_files.txt
Example of batch file which fixes a corrupted Firefox toolbar problem:

See the Nutshell Guide description of the problem here.

See the Nutshell Guide batch file here.


Find a link that's no longer valid?   Please let us know.
Please Note: The information presented online in the "In A Nutshell" series, is offered as a free, and hopefully, helpful service.
Time constraints, however, make it impossible to provide specific answers to individual inquiries.