![How to Create a Batch (.bat) File in Windows](https://i.ytimg.com/vi/sODXtX7jdcI/hqdefault.jpg)
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 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.
![](https://a.vvvvvv.in.ua/society/kak-napisat-batnik-1.webp)
- 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.
![](https://a.vvvvvv.in.ua/society/kak-napisat-batnik-2.webp)
MKDIR c: catalog1 MKDIR c: catalog2
![](https://a.vvvvvv.in.ua/society/kak-napisat-batnik-3.webp)
@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.
![](https://a.vvvvvv.in.ua/society/kak-napisat-batnik-4.webp)
@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
![](https://a.vvvvvv.in.ua/society/kak-napisat-batnik-5.webp)
Part 2 of 2: How to Save a Batch File
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 Click on File. It's in the upper-left corner of the Notepad window. A dropdown menu will open.
3 Click on Save as. It's in the File drop-down menu. The "Save As" window will open.
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 Open the dropdown menu "File Type". You will find it at the bottom of the Save As window.
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 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 Click on Save. It's in the lower-right corner of the Save As window. The window will close.
9 Close Notepad. The file will be saved as a batch file in the specified folder.
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).