Before Windows became our preferred user interface, everything was done with commands. Some of our readers may recall using MS-DOS commands to do the smallest of tasks. Today, you can still use commands to automate tasks and increase your productivity.
If you have several repetitive tasks, you can write a batch file to automate the process. Read on for some useful batch files that you can use to automate your life!
What is a batch file?
A batch file is a type of script that contains a series of commands. The batch file can contain any number of commands. As long as the operating system recognizes the commands in the script, the batch file executes the commands from start to finish.
How to create a batch file
You write batch files in plain text. You can use any text editor you want, but the standard Notepad app does the job just fine. If you're creating a complex batch file, the additional features of Notepad ++ come in handy. For now, however, you can stick to Notepad as each of the following sample batch files have been tested with this program.
The 6 best Windows Notepad alternatives
Windows Notepad is too simple, but Microsoft Office is overdone? If you're looking for a Notepad replacement, here are the top alternatives. One will be perfect for you.
When you're done entering your batch file commands, go to File> Save AsGive your batch file an appropriate name. After saving, you can change the file extension from .TXT to .Batwhich changes the file type. To do this, right-click the file and select RenameThen change the file extension as above. Alternatively, you can highlight the file and press F2, then change the file extension,
Useful Windows batch files for automation
Here are some really useful batch files to play around with, as well as some brief descriptions of what each command syntax and parameters do.
1. Open multiple programs using a batch file
If you have a list of programs that you open every time you start your computer, you can use a batch file to automate the process. Instead of opening each program manually, you can open them at the same time.
In the example below, I'm opening the Google Chrome browser, a Word document I'm working on, and VMware Player.
Open a new text file and enter the following:
cd "C: Program Files Google Chrome Application "
start – "C: Program Files Microsoft Office Office15 WINWORD.EXE"
"C: Work MUO Batch Rename.docx"
cd "C: Program Files (x86) VMware VMware Player"
You can add as many applications and files as you want to the batch file. The batch file commands in this file are:
- @Echo Displays the command that is currently being executed in a command shell. We shot that out.
- CD changes the directory.
- begin does the obvious and starts the program.
2. Delete files older than a certain time using a batch file
You can use a batch file to find and then delete files that are older than a certain number of days. You set the maximum age range for the files in the batch file so you can customize the process. In addition, you can use the batch file script to delete a specific type of file or group of files in a folder, as long as they meet the criteria specified in the commands.
The first example deletes files in the specified folder that are older than three days:
forfiles / p C:
ame here "/ s / m * / d -3 / c" cmd / c del @path "
In the second example, only files with the file extension .docx that are older than three days are deleted:
forfiles / p C:
ame here "/ s / m * .docx / d -3 / c" cmd / c del @path "
The batch file commands and switches used here are:
- forfiles allows us to use commands for every file in one location, i.e. H. the commands apply to any file that matches the command arguments
- / p specifies the path to start the search, d. H. the directory from which you want to delete the files
- / s Instructs the command to search subdirectories
- / m instructs the command to use the specified search mask. We used the wildcard operator "*" in our first example and given .docx in this second
- / d-3 is the time setting. Increase or decrease depending on your needs
- / c del @path is the delete aspect of the command
3. Automate the system backup using a batch file
You can use a batch file to back up a specific folder or as part of a larger backup configuration. You should use system backup and system restore points as part of your regular system maintenance. Sometimes it's worth making a few copies of anything that could make you cry if it were erased or destroyed.
There are many different batch file backup methods that you can use. Below are instructions for a basic backup batch file and another slightly more advanced version.
Batch File Backup Automation: Method 1
Open the editor and enter the following commands:
ROBOCOPY C: your file name goes here C: your backup storage location goes here /LOG:backuplog.txt
Now go to File> Save AsRename the file systembackup.bat and finish saving.
The simple backup method is best for backing up individual folders, but is not entirely practical for more complex applications. The batch file commands used here are:
Batch File Backup Automation: Method 2
This time, you'll create a longer string to back up, including your system registry and other important folders.
set drive = X: Backup
set backupcmd = xcopy / s / c / d / e / h / i / r / y
echo ### Backing up my documents …
% backupcmd% "% USERPROFILE% My Documents" "% drive% My Documents"
echo ### save favorites …
% backupcmd% "% USERPROFILE% Favorites" "% drive% Favorites"
echo ### Back up email and address book …
% backupcmd% "% USERPROFILE% Application Data Microsoft Address Book" "% Drive% Address Book"
% backupcmd% "% USERPROFILE% Local Settings Application Data Identities" "% drive% Outlook Express"
echo ### Backing up emails and contacts (MS Outlook) …
% backupcmd% "% USERPROFILE% Local Settings Application Data Microsoft Outlook" "% drive% Outlook"
echo ### Back up the registry …
if not available "% drive% Registry" mkdir "% drive% Registry"
if available "% drive% Registryegbackup.reg" del "% drive% Registryegbackup.reg"
regedit / e "% drive% Registryegbackup.reg"
Echo backup completed!
Here is an explanation of what the commands in this batch file mean and which bits you can customize.
First, determine the location where you want to copy the files set drive = X: Backup. In the example, the drive is set to "X". You should change this letter to the letter of your external backup drive.
The next command sets the specific type of backup copy your batch file will use. In this case xcopy. The xcopy command is followed by a number of parameters that contain additional tasks:
- / s copies system files
- / c executes the command specified by the string and then exits
- / d Enables drive and directory changes
- / e copies empty directories
- / H copies hidden files
- /I If the destination doesn't exist and you're copying more than one file, / i assumes the destination must be a directory
- / r overwrites read-only files
- / y Suppresses prompts that confirm that you want to overwrite read-only files
If you want to add more backup locations to the batch file, use the following command:
% backupcmd% "… source directory …" "% drive% … target directory …"
The batch file contains several folders to be copied. You may find that the folders span different parts of your Windows user profile. You can back up the entire folder with the following command provided you are using the same "set drive" and "set backupcmd".
% backupcmd% "% USERPROFILE%" "% drive% % UserName% – profile"
Batch File Backup Automation: Method 3
The final script to automate the backup of batch files is very simple. This creates a backup copy of a folder on an external drive and shuts down the computer when finished.
In a new text file, enter the following commands:
Robocopy "C: your folder" "X: your backup folder" / MIR
Shut down -s -t 30
Save the batch file and remember to change the file extension to .Bat. The additional batch file commands used here are:
- Robocopy / MIR: You've already taken Robocopy for a spin. The additional / me The parameter ensures that every folder and subfolder is also copied.
- Shut down -s -t: The shutdown command tells Windows that you want to shut down during this time -s Confirms that it is a full shutdown (rather than a reboot or hibernation). The -t This parameter allows you to set a specific amount of time, in seconds, before the system begins to shut down. In this example the timer is set to 30 seconds. You can change it as you wish. The shutdown process is started immediately by removing the timer parameter.
When you run the batch file, a backup copy of the defined files and folders is made and then your computer is shut down.
4. Change your IP address using a batch file
In most cases, your computer uses a dynamic IP address to connect to the Internet. Sometimes you use a static IP address instead, e.g. B. at work, at school or otherwise. Of course, you can manually switch between a dynamic and a static IP address. But if it's a place you visit regularly, why not create a batch file to do the job for you?
To create one batch file to switch to a static IP address and another to switch back to dynamic:
Batch file to switch to the static IP address
Open a new text file and copy it with the following command:
IP address of Netsh interface "LAN" static "xxx.xxx.xxx.xxx" "xxx.xxx.xxx.x" "xxx.xxx.xxx.x"
Where the first series of "x"is your required static IP, the second is the network / subnet mask, and the third is your default gateway.
Batch file for changing to the dynamic IP address
If you want to revert to a dynamic IP address, you can use this batch file.
Open another text file and copy it with the following command:
netsh int ip set address name = "LAN" source = dhcp
If you have more than one network that you connect to regularly, duplicate the first file and edit the details accordingly.
5. Let your kids go to bed with a batch file
My kids aren't old enough to play video games in the middle of the night, but I remember my tactics against my parents so I could play Championship Manager 2 until the wee hours of the morning. Fortunately, my parents didn't know how to use commands to control my actions.
You can set an alert and start a countdown timer on your child's computer using the following batch file:
If% time% == 23: 30: 00.00 go to: X.
shutdown.exe / s / f / t / 120 / c "GO TO BED NOW !!!"
Here the computer constantly checks whether the time is eleven thirty. If the time correlates, the message "GO TO BED NOW !!!" is displayed along with the 120s countdown timer. The 120s should have enough time to save the game or work before shutting down the computer.
Press to stop the countdown Windows key + R.. (Of course, don't tell the kids that!)
6. Batch renaming and bulk deletion of files
I've written a more in-depth article on renaming and deleting batch files so I don't dig into it too deeply. However, you can use batch files to automate these sometimes tedious tasks. Check out this article for some advanced batch commands. You can delete the masses immediately.
7. Play Pokémon in a batch file
This batch file has nothing to do with productivity. In fact, it is the complete opposite. If you are prone to Pokémon-related gambling addiction, you should make this one mistake, as it is essentially textual Pokémon Red.
If you don't want to miss out, you can take PokéBatch and start playing. Download the text file and change the file extension from .TXT to .Batand you're good to go
If you like a challenge, check out the most fun Pokémon Challenges to prove your mastery on the series.
Automate your life with Windows batch files!
These are just six batch files you can create to automate tasks on your system. With more practice, you can perform unannounced activities on your system between batch files and the command prompt.
5 reasons you should use PowerShell instead of batch scripting
Most users have never heard of PowerShell, let alone touched and tried it. Perhaps this is because of the name, which sounds like something only used by hardcore IT geeks. Or maybe because …
About the author
(589 articles published)
Gavin is the junior editor for Windows and Technology Explained, contributing regularly to the Really Useful Podcast, and was the editor for MakeUseOf's crypto-focused sister site Blocks Decoded. He has a BA (Hons) in Contemporary Writing Using Digital Art Practices Looted from the Devon Hills, as well as over a decade of writing experience. He enjoys plenty of tea, board games, and soccer.
More from Gavin Phillips
Subscribe to our newsletter
Subscribe to our newsletter for tech tips, reviews, free e-books, and exclusive deals!
One more step …!
Please confirm your email address in the email we just sent you.