In this article, you will learn how to write and save the simplest batch file (batch file) on a computer running Windows. 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.
Part 1 of 2: The Basics
Step 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 notepad 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
- ECHO: displays text on the screen;
- @ECHO OFF: hides the displayed text;
- START: starts the file with the application;
- REM: adds a line with comments;
- MKDIR / RMDIR: creates and removes 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.
MKDIR c: \ catalog1 MKDIR c: \ catalog2
@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
@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 is the variable FOR %% f IN (*.jpg *.png *.bmp) DO XCOPY C: \ source \ " %% f "c: \ images / m / y REM All files with extension.jpg,.png,.bmp REM will be copied from c: \ source to c: \ images
Part 2 of 2: How to Save a Batch File
Step 1. Finish entering the batch file program
When you are finished entering and editing the batch file program, save it as an executable file.
Step 2. Click on File
It's in the upper-left corner of the Notepad window. A dropdown menu will open.
Step 3. Click Save As
It's in the File drop-down menu. The "Save As" window will open.
Step 4. Enter the name and extension.bat
In the File Name box, enter a name, and then enter.bat.
For example, if the batch file to be backed up will be named "backup", enter backup.bat
Step 5. Open the dropdown menu "File Type"
You will find it at the bottom of the Save As window.
Step 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).
Step 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.
Step 8. Click Save
It's in the lower-right corner of the Save As window. The window will close.
Step 9. Close Notepad
The file will be saved as a batch file in the specified folder.
Step 10. Modify the batch file program
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 pressing Ctrl + S.
The changes will take effect as soon as you run the batch file
- If the 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).