How to Write a Batch File: 10 Steps (with Pictures) - wikiHow (2024)

Download Article

Explore this Article

parts

1Learning Batch Basics

2Saving the Batch File

Sample Batch File

Other Sections

Tips and Warnings

Related Articles

References

Article Summary

Written byJack Lloyd

Last Updated: December 2, 2023Tested

Download Article

This wikiHow teaches you how to write and save a basic batch file on a Windows computer. A batch file contains a series of DOS (Windows language) commands, and is commonly written to automate frequently performed tasks such as moving files. You shouldn't have to download any fancy editors to create a batch file—the Windows-standard Notepad program is more than sufficient.

Part 1

Part 1 of 2:

Learning Batch Basics

Download Article

  1. 1

    Open Notepad. Notepad allows you to create code as a text file and then save it when you're done as a batch file. You can open Notepad by opening Start

    , typing in Notepad, and clicking the blue Notepad app icon at the top of the menu.

    • Notepad is commonly used to convert text files into batch files, but you can write your batch file's text virtually anywhere.
  2. 2

    Learn some basic batch commands. Batch files run a series of DOS commands, so the commands that you can use are similar to DOS commands. Some of the more important ones include:

    • ECHO - Displays text on the screen
    • @ECHO OFF - Hides the text that is normally output
    • START - Run a file with its default application
    • REM - Inserts a comment line in the program
    • MKDIR/RMDIR - Create and remove directories
    • DEL - Deletes a file or files
    • COPY - Copy a file or files
    • XCOPY - Allows you to copy files with extra options
    • FOR/IN/DO - This command lets you specify files.
    • TITLE- Edit the title of the window.[1]

    Advertisem*nt

  3. 3

    Write a program to create a directory. One of the easiest ways to learn how to create batch files is to focus on doing basic tasks first. For example, you can use a batch file to quickly create multiple directories:</ref>

    MKDIR c:\example1MKDIR c:\example2
  4. 4

    Write the code to make a basic backup program. Batch files are great for running multiple commands, especially if you configure it to be able to run multiple times. With the XCOPY command, you can make a batch file that copies files from select folders to a backup folder, only overwriting files that have been updated since the last copy:

    @ECHO OFF XCOPY c:\original c:\backupfolder /m /e /y
    • This copies over files from the folder "original" to the folder "backupfolder". You can replace these with the paths to the folders you want. /m specifies that only updated files will be copied, /e specifies that all subdirectories in the listed directory will be copied, and /y keeps the confirmation message appearing every time a file is overwritten.
  5. 5

    Write a more advanced backup program. While simply copying the files from one folder to another is nice, what if you want to do a little sorting at the same time? That's where the FOR/IN/DO command comes in. You can use that command to tell a file where to go depending on the extension:

    @ECHO OFF cd c:\sourceREM This is the location of the files that you want to sortFOR %%f IN (*.doc *.txt) DO XCOPY c:\source\"%%f" c:\text /m /yREM This moves any files with a .doc or REM .txt extension from c:\source to c:\textREM %%f is a variableFOR %%f IN (*.jpg *.png *.bmp) DO XCOPY C:\source\"%%f" c:\images /m /yREM This moves any files with a .jpg, .png, REM or .bmp extension from c:\source to c:\images
  6. 6

    Display some text. If you want to know what is happening in your batch file but don't want to see all the commands, you could program the batch file to print some text that explains what the batch file does. You can print text with ECHO. For example:

    @ECHO OFFMKDIR c:\example1ECHO Created directory example1
    • You can change the color of the output with COLOR bf, where b is the background and f is the foreground color, both a hexadecimal number. Following colors are possible:
      NumberColorNumberColor
      0

      black

      8

      dark grey

      1

      dark blue

      9

      blue

      2

      dark green

      a

      green

      3

      dark turquoise

      b

      turquoise

      4

      dark red

      c

      red

      5

      dark magenta

      d

      magenta

      6

      dark yellow

      e

      yellow

      7

      light grey

      f

      white

    • For example, red text on a dark green background would be displayed with
      COLOR 2c
    • You need run the batch file from the command line to see the text, because else the window will close too fast to actually read the text you printed.
  7. 7

    Experiment with different batch commands. If you want inspiration, you can check out the sample batch text at the end of this article.

  8. Advertisem*nt

Part 2

Part 2 of 2:

Saving the Batch File

Download Article

  1. 1

    Finish entering your batch file's text. Once you've completed and proofread your batch file, you can proceed with saving it as an executable file.

  2. 2

    Click File. It's in the top-left corner of the Notepad window. A drop-down menu will appear.

  3. 3

    Click Save As…. This option is in the File drop-down menu. Clicking it prompts the Save As window to open.

  4. 4

    Enter a name and the ".bat" extension. In the "File name" text box, type in whatever you want to name your program followed by .bat.

    • For a program named "Backup", for example, you'd type in Backup.bat here.
  5. 5

    Click the "Save as type" drop-down box. You'll find it near the bottom of the Save As window. A drop-down menu will appear.

  6. 6

    Click All Files. It's in the drop-down menu. This will allow your file to be saved as whatever its extension is (in this case, ".bat").

  7. 7

    Select a save location. Click a folder on the left side of the window (e.g., Desktop) to do so.

  8. 8

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

  9. 9

    Close your Notepad file. It will be saved as a batch file in your selected location.

  10. 10

    Edit the batch file's contents. At any time, you can right-click your batch file and click Edit in the resulting drop-down menu. This will open the batch file as a Notepad document; at this point, you can make any changes and then save the file by pressing Ctrl+S.

    • The changes will immediately be reflected when you run the batch file.
  11. Advertisem*nt

Sample Batch File

Sample Batch File

Community Q&A

Search

Add New Question

  • Question

    How can I run multiple batch files?

    How to Write a Batch File: 10 Steps (with Pictures) - wikiHow (23)

    Community Answer

    Open multiple files at once. But if you really wanted to, you can open other batch files within a batch file as such: start "c:\Users\Xx_balzeitmichael_xX\Documents" random.bat.

    Thanks! We're glad this was helpful.
    Thank you for your feedback.
    If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission.Support wikiHow

    YesNo

    Not Helpful 16Helpful 33

  • Question

    How do I open images using a batch file?

    How to Write a Batch File: 10 Steps (with Pictures) - wikiHow (24)

    David Langr

    Community Answer

    You can do so by typing the following command: start "C:\Path\picture.jpg".

    Thanks! We're glad this was helpful.
    Thank you for your feedback.
    If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission.Support wikiHow

    YesNo

    Not Helpful 5Helpful 16

  • Question

    How do I create a batch file?

    How to Write a Batch File: 10 Steps (with Pictures) - wikiHow (25)

    Arsalan Kazmi

    Community Answer

    Enter the code in Notepad and then go to File > Save As, click File Type, set it to All Files, then in the Name box, enter name.bat, where 'name' is the name of your batch file.

    Thanks! We're glad this was helpful.
    Thank you for your feedback.
    If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission.Support wikiHow

    YesNo

    Not Helpful 9Helpful 21

See more answers

Ask a Question

200 characters left

Include your email address to get a message when this question is answered.

Submit

      Advertisem*nt

      Tips

      • You will have to use quotes if you want to open a directory or file with spaces in its name (e.g., start "C:\Documents and Settings\").

        Thanks

        Helpful0Not Helpful0

      • You can use third-party editors such as Notepad++ to edit your batch file, but for the most part, these are a waste of time when writing simple batch files.

        Thanks

        Helpful0Not Helpful0

      • Some commands (such as ipconfig) will need administrative permissions to work. You can right-click the file and then click "Run as Administrator" to grant this permission if you're on an administrator account.

        Thanks

        Helpful0Not Helpful0

      Submit a Tip

      All tip submissions are carefully reviewed before being published

      Submit

      Thanks for submitting a tip for review!

      Advertisem*nt

      Warnings

      • Depending on the commands you use, batch files can be dangerous. Make sure that none of your code runs the risk of performing an undesirable task (e.g., deleting files or crashing your computer).

        Thanks

        Helpful2Not Helpful0

      Advertisem*nt

      You Might Also Like

      How to Take a Screenshot on a Windows PC: 8 Simple Tricks
      How toEnter BIOS4 Easy Ways to Access the Boot Options Menu in WindowsEasily Download and Install FFmpeg on a Windows PCHow to Find Your Windows Product KeyHow toCheck Your Windows VersionHow toAutomatically Shut Down Your Computer at a Specified TimeInstalling Windows After Ubuntu: A Step-by-Step Guide2 Ways to Turn Off S Mode on a Windows 11 PCHow toZoom Out on a PC3 Ways to Change a Computer's Mac Address in Windows2 Ways to Install Windows Apps if You're Not an Administrator

      Advertisem*nt

      About This Article

      How to Write a Batch File: 10 Steps (with Pictures) - wikiHow (41)

      Written by:

      Jack Lloyd

      wikiHow Technology Writer

      This article was co-authored by wikiHow staff writer, Jack Lloyd. Jack Lloyd is a Technology Writer and Editor for wikiHow. He has over two years of experience writing and editing technology-related articles. He is technology enthusiast and an English teacher. This article has been viewed 2,961,142 times.

      How helpful is this?

      Co-authors: 98

      Updated: December 2, 2023

      Views:2,961,142

      Categories: Featured Articles | Windows

      Article SummaryX

      1.Learn basic batch commands.
      2.Open Notepad.
      3.Write your program.
      4.Go to File > Save As.
      5.Type a name for the file that ends with ".bat"
      6.Select All Files from the "Save as type" menu.
      7.Click Save.

      Did this summary help you?

      In other languages

      Español:crear un archivo por lotes

      Français:écrire un fichier batch

      Deutsch:Eine Batch Datei schreiben

      Русский:написать батник

      Nederlands:Een batchbestand maken

      Bahasa Indonesia:Menulis Berkas Batch

      中文:编写批处理文件

      हिन्दी:बैच फाइल (Batch File) लिखें

      العربية:كتابة ملفات Batch

      한국어:배치파일 만드는 법

      ไทย:เขียน Batch File

      Tiếng Việt:Viết tập tin batch

      • Print
      • Send fan mail to authors

      Thanks to all authors for creating a page that has been read 2,961,142 times.

      Is this article up to date?

      How to Write a Batch File: 10 Steps (with Pictures) - wikiHow (2024)

      References

      Top Articles
      Latest Posts
      Article information

      Author: Van Hayes

      Last Updated:

      Views: 6443

      Rating: 4.6 / 5 (46 voted)

      Reviews: 93% of readers found this page helpful

      Author information

      Name: Van Hayes

      Birthday: 1994-06-07

      Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

      Phone: +512425013758

      Job: National Farming Director

      Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

      Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.