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)

Video: How to Write a Batch File: 10 Steps (with Pictures)

Video: How to Write a Batch File: 10 Steps (with Pictures)
Video: How to Safely Use Microsoft Windows XP FOREVER! 2024, December
Anonim

This wikiHow teaches you how to write and save a basic batch file on a Windows computer. This file contains a series of DOS (Windows language) commands and is usually created to automatically activate frequently used commands, such as file transfer. You don't need to download a powerful editing program to create batch files; Windows default Notepad program is more than enough.

Step

Part 1 of 2: Learning Batch File Basics

4288 1 2
4288 1 2

Step 1. Open Notepad

This program allows you to generate code as a text file and save it when finished as a batch file. To access the program, go to the menu “ Start

Windowsstart
Windowsstart

type Notepad, and click the “ Notepad ” in blue at the top of the menu.

Notepad is usually used to convert text files to batch files. However, you can write batch file text through any application

  • Learn some basic batch file commands. Batch files can run a series of DOS commands. This means that the command you are using is similar to a DOS command. Some of the important commands include:

    4288 2 2
    4288 2 2
    • ECHO – Displays text on the screen
    • @ECHO OFF – Hides normally displayed text
    • START – Runs files through the main application
    • REM – Inserts a comment line in the program
    • MKDIR/RMDIR – Creates and deletes directories
    • DEL – Delete files (either single or multiple)
    • COPY – Copy one or more files
    • XCOPY – Allows you to copy files with additional options
    • FOR/IN/DO – Allows you to select or specify a file.
    • TITLE- Edits the title of the program window.
  • Write a directory creation program. One of the easiest ways to learn how to create batch files is to focus on basic commands or tasks first. For example, you can use a batch file to quickly create multiple directories:

    4288 3 2
    4288 3 2

    MKDIR c:\example1 MKDIR c:\example2

  • Write code to create a basic backup program. Batch files are a great medium for running multiple commands, especially if you've configured them to run multiple times. With the XCOPY command, you can create a file that copies files from the selected folder to the backup folder, and only overwrites files that have been updated since the last copying session:

    4288 4 2
    4288 4 2

    @ECHO OFF XCOPY c:\original c:\backup folder /m /e /y

    This command works to copy files from the "original" folder to the "backup" folder. You can replace the folder with the address of the desired folder or directory. “/m” indicates that only updated files will be copied, “/e” indicates that all subdirectories in the selected directory will be copied, and “/y” displays a confirmation message whenever an old file is overwritten by a new file

  • Write a more advanced backup program. Creating a file that functions to copy files from one folder to another is certainly fun. However, what if you want to do the sorting at the same time? For situations like this, the FOR/IN/DO command will be very helpful. You can use these commands to specify or tell where to send/copy files based on their extensions:

    4288 5 2
    4288 5 2

    @ECHO OFF cd c:\REM sources This is the location of the source files to sort FOR %%f IN (*.doc *.txt) DO XCOPY c:\source\"%%f" c:\text /m /y REM This command moves files with the extension.doc or REM.txt from the c:\sources folder to the c:\text folder REM %%f is a variable FOR %%f IN (*.jpg *.png *.bmp) DO XCOPY C:\source \"%%f" c:\images /m /y REM This command moves files with the extension.jpg,.png, REM or.bmp from the c:\sources folder to the c:\images folder

  • Experiment with different batch commands. If you need inspiration, you can look up batch text examples on the internet.

    4288 6 2
    4288 6 2
  • Part 2 of 2: Saving Batch Files

    4288 7 2
    4288 7 2

    Step 1. Finish the text file

    After finishing and checking the command text, you can save it as an executable file.

    4288 8 2
    4288 8 2

    Step 2. Click File

    It's in the upper-left corner of the "Notepad" window. A drop-down menu will appear after that.

    4288 9 2
    4288 9 2

    Step 3. Click Save As…

    This option is in the drop-down menu “ File Once clicked, the “Save As” window will be displayed.

    4288 10 2
    4288 10 2

    Step 4. Enter the file name and extension ".bat"

    In the " File name " field, type the name of the program you want, followed by the.bat extension.

    For a program called "Backup," for example, type Backup.bat into this field

    4288 11 2
    4288 11 2

    Step 5. Click the “Save as type” drop-down box

    This box is at the bottom of the “Save As” window. A drop-down menu will be displayed.

    4288 12 2
    4288 12 2

    Step 6. Click All Files

    This option is in the drop-down menu. With this option, the file can be saved as any extension you specify (in this case, ".bat").

    4288 13 2
    4288 13 2

    Step 7. Select a save location

    Click a folder on the left side of the window (e.g. Desktop ”) to select a location.

    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 after that.

    4288 15 2
    4288 15 2

    Step 9. Close the Notepad file

    The file will be saved as a batch file in the selected location.

    4288 16 2
    4288 16 2

    Step 10. Edit the contents of the batch file

    Whenever needed, you can right-click on the file and select “ Edit ” from the drop-down menu. After that, the file will be opened as a Notepad document. At this point, you can make changes and save the file by pressing the keyboard shortcut Ctrl+S.

    The changes will take effect immediately when you run the file

    Tips

    • You need to use quotation marks if you want to use directories or files that have spaces in their names e.g. start "C:\Documents and Settings\").
    • You can use a third-party text-editing program such as Notepad++ to edit batch files, but usually using such a program is a waste of time if you only need to write a simple batch file.
    • Some commands (eg ipconfig) require administrative permissions to run. You can right click the file and select " Run as Administrator " to grant permissions as long as you are using an administrator account.

    Recommended: