This article shows you how to create a simple batch file and then run it on any Windows system. Batch files consist of a sequence of MS-DOS commands (a language dedicated to Windows operating systems) and are often used to automate actions, for example to move or copy a series of files from one folder to another. In order to create a batch file, you don't need to use any additional program or software, just a normal text editor like the classic Windows "Notepad".
Steps
Part 1 of 2: Learning the Basics of Creating a Batch File
Step 1. Launch the Notepad program
It is a simple text editor integrated in all versions of Windows that allows you to write code as if it were simple text and then save it as a batch file. To start the Notepad editor access the menu Start clicking the icon
type in the keywords notepad, then select its blue icon Block notes appeared at the top of the results list.
The Notepad program is often used to write a text file containing the set of DOS commands that will be part of the batch file and save it in this format. However, if you wish, you can create your own code using any tool at your disposal
Learn what basic commands can be included in a batch file. The main purpose of the latter is precisely to automatically execute a predefined sequence of DOS commands, so the commands you can use are exactly those that can be executed within the Windows "Command Prompt". Here is a short list of the most important:
- ECHO - display text on screen;
- @ECHO OFF - hides the text that would normally be displayed on the screen as a result of the execution of a command;
- START - runs a file using the system default application;
- REM - inserts a comment line into the program code;
- MKDIR / RMDIR - create and delete a directory;
- DEL - delete a file;
- COPY - copy a file;
- XCOPY - allows you to copy a file by specifying additional options;
- FOR / IN / DO - allows you to execute a specific command for a series of files;
- TITLE - change the title of the window;
Write a program to create a new directory. One of the simplest ways to learn how to create a batch file is to get some experience with basic operations. For example, you can use a batch file to automatically create a series of folders:
MKDIR c: / Example_1 MKDIR c: / Example_2
Create the code to make a simple backup program. Batch files are perfect for running a sequence of multiple commands and are especially ideal when that sequence needs to be run periodically and repeatedly. Using the "XCOPY" command, you are able to create a batch file that copies the files in certain directories into a backup folder and only overwrites files that have changed since the 'last run of the program:
@ECHO OFF XCOPY c: / source_directory c: / backup / m / e / y
This simple command copies the files in the "source_directory" folder into the "backup" directory. By replacing these two parameters with the desired folder paths you can back up your personal data. The / m parameter instructs you to copy only files that have changed. The / e parameter specifies that all existing subfolders should also be copied, while the / y parameter requires user confirmation before overwriting a file that already exists in the destination folder
Create a more advanced schedule. While copying a file from one folder to another is already very satisfying, why not organize them while copying? In this case, the ideal solution is to use the "FOR / IN / DO" command. For example, you can use it to tell the program to sort files into separate folders based on extension:
@ECHO OFF cd c: / source REM This is the folder where the files to be reorganized are stored FOR %% f IN (*.doc *.txt) DO XCOPY c: / source / "%% f" c: / File_Testo / m / y REM this command copies text files with.doc or REM.txt extension from the c: / source folder to the c: / REM Text_File directory the parameter %% f is a variable FOR %% f IN (*.jpg *.png *.bmp) DO XCOPY C: / source / "%% f" c: / Images / m / y REM this command copies all files with the extension.jpg,.png REM or.bmp from the folder c: / source to the directory c: / Images
Practice using different DOS commands. If you need to find inspiration, simply search online using the keywords "batch commands" and "create batch files".
Part 2 of 2: Saving a Batch File
Step 1. Complete the creation of the text document containing the batch file code
After creating and checking the code of your batch file, you can proceed to create the actual executable file.
Step 2. Access the File menu
It is located in the upper left of the "Notepad" program window. A drop-down menu will appear.
Step 3. Choose the Save As… option
It is one of the items in the menu File. This will bring up the "Save As" system window.
Step 4. Name the file and add the ".bat" extension
Inside the "File Name" text field, type the name you want to give your batch file followed by the.bat extension.
For example if your program is called "Backup" as the name for its batch file, you could choose Backup.bat and enter it in the "File name" field
Step 5. Access the "Save As" drop-down menu
It is visible at the bottom of the dialog box of the same name, under the "File name" text field.
Step 6. Choose the All Files (*. *) Option
It is one of the items in the drop-down menu that appeared. This way you will be able to give the file the extension you prefer (in this case ".bat").
Step 7. Select the destination folder
Choose the directory where you want to save the batch file you just created. Use the left sidebar of the "Save As" window. For example you can choose to save it directly to the Desktop.
Step 8. Press the Save button
It is located in the lower right corner of the "Save As" window. The latter will be closed and the file will be saved in the indicated folder.
Step 9. Close the "Notepad" program
The document you created was saved as a batch file in the selected directory.
Step 10. Edit the code of your batch file
At any time, if you need to make changes to the program's source code, you can select the relevant batch file with the right mouse button and choose the option Edit from the context menu that appeared. The content will automatically appear in the default text editor window, for example "Notepad". At this point you can make any changes you want and save the file by simply pressing the key combination Ctrl + S.
The changes will be made effective and you can test their validity by running the relevant batch file again
Advice
- If you have entered commands in the batch file to access directories or open files whose names contain empty spaces you will need to enclose them in quotation marks (for example start "C: / Documents and Settings \").
- To create or edit a batch file you can use a third-party text editor such as Notepad ++. However, in most cases where you are dealing with simple batch files, it is more than enough to use the classic Windows "Notepad".
- Some commands (for example the "ipconfig" command), to be executed correctly, require a system administrator account. If you are logged in to Windows with a normal user account, you can right-click the batch file you created and choose the "Run as administrator" option from the context menu that appeared.