How to Write a Batch File

Part 1 of 2:

Learning Batch Basics

  1. How to Write a Batch File Picture 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
    How to Write a Batch File Picture 2
    , typing in Notepad, and clicking the blue Notepad app icon at the top of the menu.
    1. Notepad is commonly used to convert text files into batch files, but you can write your batch file's text virtually anywhere.
  2. How to Write a Batch File Picture 3
    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:
    1. ECHO - Displays text on the screen
    2. @ECHO OFF - Hides the text that is normally output
    3. START - Run a file with its default application
    4. REM - Inserts a comment line in the program
    5. MKDIR/RMDIR - Create and remove directories
    6. DEL - Deletes a file or files
    7. COPY - Copy a file or files
    8. XCOPY - Allows you to copy files with extra options
    9. FOR/IN/DO - This command lets you specify files.
    10. TITLE- Edit the title of the window. [1]
  3. How to Write a Batch File Picture 4
    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:[2]
    MKDIR c:example1 MKDIR c:example2 
  4. How to Write a Batch File Picture 5
    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 
    1. 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. How to Write a Batch File Picture 6
    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:source REM This is the location of the files that you want to sort FOR %%f IN (*.doc *.txt) DO XCOPY c:source"%%f" c:text /m /y REM This moves any files with a .doc or  REM .txt extension from c:source to c:text REM %%f is a variable FOR %%f IN (*.jpg *.png *.bmp) DO XCOPY C:source"%%f" c:images /m /y REM This moves any files with a .jpg, .png,  REM or .bmp extension from c:source to c:images 
  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 OFF MKDIR c:example1 ECHO Created directory example1 
    1. 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:NumberColorNumberColor0

      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

    2. For example, red text on a dark green background would be displayed with
      COLOR 2c 
    3. 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. How to Write a Batch File Picture 7
    Experiment with different batch commands. If you want inspiration, you can check out the sample batch text at the end of this article.
Score
0 / 0

Part 1 Quiz

If you want to add a section to your batch code that only copies updated files, what should you add to the code?

That's right! Adding the /m function will ensure only updated files are copied. Using /m in a basic program will allow your batch file to efficiently back up your data. Read on for another quiz question.

Not quite! The /e function won't work to keep only updated files. Instead, use /e to specify that all your subdirectories in any listed directory should be copied. Choose another answer!

Try again! Using /y won't limit your backups to only updated files. Instead, you can insert /y into your batch file to allow the confirmation message to pop up each time a file is overwritten. Try another answer...

Want more quizzes?

Keep testing yourself!
Part 2 of 2:

Saving the Batch File

  1. How to Write a Batch File Picture 8
    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. How to Write a Batch File Picture 9
    Click File. It's in the top-left corner of the Notepad window. A drop-down menu will appear.
  3. How to Write a Batch File Picture 10
    Click Save As…. This option is in the File drop-down menu. Clicking it prompts the Save As window to open.
  4. How to Write a Batch File Picture 11
    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.
    1. For a program named "Backup", for example, you'd type in Backup.bat here.
  5. How to Write a Batch File Picture 12
    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. How to Write a Batch File Picture 13
    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. How to Write a Batch File Picture 14
    Select a save location. Click a folder on the left side of the window (e.g., Desktop) to do so.
  8. How to Write a Batch File Picture 15
    Click Save. It's in the bottom-right corner of the Save As window. The window will close.
  9. How to Write a Batch File Picture 16
    Close your Notepad file. It will be saved as a batch file in your selected location.
  10. How to Write a Batch File Picture 17
    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.
    1. The changes will immediately be reflected when you run the batch file.
Score
0 / 0

Part 2 Quiz

Why should you save your batch file as a .bat extension?

Not exactly! Choosing a .bat extension won't keep your notepad document as a text file. If you are not finished writing your batch file, you can save the document as a .txt extension, which will keep the document as text. Click on another answer to find the right one...

Yes! When you're finished writing your batch file, switch the extension to .bat instead of .txt. This converts your document to a batch file once it's saved under "All Files." Read on for another quiz question.

Nope! You can save your batch file under the default option of the "Save as Type" dropdown menu. However, if you don't convert the drop down option to "All Files," you typically won't be able to save your document as a batch file. Guess again!

Want more quizzes?

Keep testing yourself!

Sample Batch File

How to Write a Batch File Picture 18 Sample Batch File
5 ★ | 1 Vote

May be interested

  • File & I / O in PHPFile & I / O in PHP
    in this article we will learn about explaining file-related functions in php.
  • How to Execute batch file from command line on WindowsHow to Execute batch file from command line on Windows
    today's tipsmake will show you how to execute batch files (script files with .bat extension) from the windows command line. you can run the program from the 'run' dialog box, or type the command into a terminal window.
  • How to use File Converter to batch convert files from the right-click menuHow to use File Converter to batch convert files from the right-click menu
    file converter is a software to convert files right from the right-click menu without you having to access the application to convert file formats.
  • Learn about the Write Zero methodLearn about the Write Zero method
    many file shredder and data destruction programs support data sanitization method based on write zero software to overwrite existing data on storage devices such as hard drives.
  • How to Launch BAT Files on WindowsHow to Launch BAT Files on Windows
    tipsmake today will show you how to launch bat files (also known as batch files) on windows computers. batch files are used for many purposes, such as automating frequently used tasks. you can launch the file in file explorer as usual, or execute it from the command line in the command prompt.
  • What is ISO file?What is ISO file?
    an iso file, often called an iso image, is a single file, replacing all cds, dvds or bds. the entire contents of the disc can be accurately copied in a single iso file.
  • How to batch delete files on Windows 10How to batch delete files on Windows 10
    deleting anything more than a few files with file explorer can become a lengthy process. when it comes to batch deletion of files on windows 10, you have a few options.
  • Read - Write File in CRead - Write File in C
    the previous chapter explained the standard input and output devices handled by the c language. in this chapter we will see how programmers create, open and close text files or binary files with data. storage.
  • Read / write File in C ++Read / write File in C ++
    so far, we have used the iostream standard library, provided cin and cout methods to read from standard input and write to the corresponding standard output.
  • Instructions for batch compressing images on Batch Image CompressorInstructions for batch compressing images on Batch Image Compressor
    batch image compressor will help you batch compress images when we can download multiple files at once to reduce capacity.