How to write a batch file

Author: Gregory Harris
Date Of Creation: 12 August 2021
Update Date: 1 July 2024
Anonim
How to Create a Batch (.bat) File in Windows
Video: How to Create a Batch (.bat) File in Windows

Content

In this article, you will learn how to write and save the simplest batch file (batch file) on a Windows computer. A batch file contains a number of DOS (operating system) commands and is commonly used to automate frequently performed tasks such as moving files. To create a batch file, you don't need complex software - you only need the Notepad text editor.

Steps

Part 1 of 2: The Basics

  1. 1 Open Notepad. In Notepad, you can write a program as a text file and then save it as a batch file. To open Notepad, open the Start menu , enter notebook and click on the blue Notepad icon at the top of the menu.
  • Notepad is used to convert text files to batch files, but batch file code can be written in any text editor.
  • Remember some basic commands. The batch file runs DOS commands, so the commands used are the same as DOS commands. Some of the more important commands are:
    • ECHO: displays text on the screen;
    • @ECHO OFF: hides the displayed text;
    • START: launches the file with the application;
    • REM: adds a line with comments;
    • MKDIR / RMDIR: creates and deletes directories;
    • DEL: deletes files;
    • COPY: copies files;
    • XCOPY: copies files with additional parameters;
    • FOR / IN / DO: defines files;
    • TITLE: edits the title of the window.
  • Write a program to create a directory. The quickest way to learn how to create batch files is to start with the simplest tasks. For example, use a batch file to quickly create multiple directories:

    MKDIR c: catalog1 MKDIR c: catalog2

  • Write a program to create a backup. Batch files are great for running multiple commands at once, especially if those commands need to be run multiple times. Using the XCOPY command, you can create a batch file that copies the files from the specified folders to the backup folder, and only those files that have changed since the last copy will be overwritten:

    @ECHO OFF XCOPY c: original c: backupfolder / m / e / y

    • This program will copy the files from the "original" folder to the "backupfolder" folder. These folders can be replaced with others (with corresponding folder paths). / m indicates that only modified files will be copied; / e indicates that all subfolders (which are in the specified folder) will be copied; / y prompts you to overwrite the file.
  • Write a more complex backup program. Simply copying files from one folder to another makes it easier to work with your computer, but what if you sort the files while copying them? This requires the FOR / IN / DO command. Use this command to specify which folder the file should be copied to, depending on its extension:

    @ECHO OFF cd c: source REM This is the source folder with the files FOR %% f IN ( *. Doc *. Txt) DO XCOPY c: source "%% f" c: text / m / y REM All files with the extension .doc or .txt REM will be copied from c: source to c: text REM %% f this variable FOR %% f IN ( *. Jpg *. Png *. Bmp) DO XCOPY C : source "%% f" c: images / m / y REM All files with extension .webp, .png, .bmp REM will be copied from c: source to c: images

  • Experiment with different commands. There are many examples of batch file programs on the Internet.
  • Part 2 of 2: How to Save a Batch File

    1. 1 Finish entering the batch file program. When you are finished entering and editing the batch file program, save it as an executable file.
    2. 2 Click on File. It's in the upper-left corner of the Notepad window. A dropdown menu will open.
    3. 3 Click on Save as. It's in the File drop-down menu. The "Save As" window will open.
    4. 4 Enter the name and extension .bat. In the File Name line, enter a name, and then enter .bat.
      • For example, if the batch file to be backed up will be named "backup", enter backup.bat.
    5. 5 Open the dropdown menu "File Type". You will find it at the bottom of the Save As window.
    6. 6 Click on All files. It's in the drop-down menu. This will save the file with the specified extension (in this case, the .bat extension).
    7. 7 Specify the folder where the batch file will be stored. To do this, click on the desired folder (for example, on "Desktop") on the left side of the window.
    8. 8 Click on Save. It's in the lower-right corner of the Save As window. The window will close.
    9. 9 Close Notepad. The file will be saved as a batch file in the specified folder.
    10. 10 Change the program of the batch file. To do this, right-click on it and select "Change" from the menu. The batch file will open in Notepad; make the necessary changes and then save the file by clicking Ctrl+S.
      • The changes will take effect as soon as you run the batch file.

    Tips

    • If a file or directory name contains spaces, enclose the name in quotation marks (for example, start "C: Documents and Settings ").
    • You can use third-party editors such as Notepad ++ to edit a batch file, but this is largely a waste of time if you are going to create basic batch files.
    • Some commands (for example, ipconfig) will require administrative privileges to run. In this case, right-click on the file and select "Run as administrator" from the menu (if you are logged in as administrator).

    Warnings

    • Batch files can be dangerous depending on the commands used. Make sure that the commands in the batch file do not cause unwanted consequences (for example, deleting files or crashing the computer).