Before Windows became our favorite GUI, everything was done using commands. Some of our readers may remember using MS-DOS commands to complete the smallest of tasks. These days, you can still use commands to automate tasks and speed up your productivity.

If you have a number of repetitive tasks, you can write a batch file to automate the process. Keep reading for several useful batch files 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. So long as the operating system recognizes the script's commands, the batch file will execute 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 like, but the standard Notepad app does the job just fine. If you're creating a complex batch file, the additional features of Notepad++ are handy. But for now, you can stick with Notepad, as each example batch file below has been tested using that program.

Once you finish inputting your batch file commands, head to File > Save As, then give your batch file an appropriate name. After saving, you can change the file extension from .txt to .bat, which changes the file type. To do this, right-click the file and select Rename, then change the file extension as above. Alternatively, highlight the file and press F2, then change the file extension,

Useful Windows Batch Files for Automation

Here are a few really useful batch files for you to play around with and some short descriptions of what each command syntax and parameter can do.

1. Open Multiple Programs Using a Batch File

If you have a list of programs you open each time you fire up your computer, you can use a batch file to automate the process. Instead of opening each program manually, you can open them simultaneously.

start programs via batch file

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 input:

          @echo            off
cd "C:\Program Files\Google\Chrome\Application\"
start chrome.exe
start – "C:\Program Files\Microsoft Office\Office15\WINWORD.EXE"
"C:\Work\MUO\How to Batch Rename.docx"
cd "C:\Program Files (x86)\VMware\VMware Player"
start vmplayer.exe
Exit

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 currently being executed in a command shell. We turned this off.
  • cd changes the directory.
  • start 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 scan for and then delete files older than a certain amount of days. You set the maximum age range for the files in the batch file, allowing you to customize the process. Furthermore, you can use the batch file script to delete a specific file type or a group of files in a folder, so long as they meet the criteria expressed in the commands.

The first example deletes files in the specified folder older than three days:

          forfiles /p            "C:            
ome\file
ame\here" /s /m * /d -3 /c "cmd /c del @path"

The second example only deletes files with the .docx file extension older than three days:

          forfiles /p            "C:            
ome\file
ame\here" /s /m * .docx /d -3 /c "cmd /c del @path"

The batch file commands and switches in use here are:

  • forfiles allows us to use commands for each file in a location i.e. the commands will apply to each file fitting the command arguments
  • /p details the path to start searching i.e. the directory you want to delete the files from
  • /s instructs the command to search sub-directories
  • /m instructs the command to use the given search mask. We used the wildcard operator "*" in our first example, and specified .docx in the second
  • /d-3 is the time setting. Increase or decrease depending on your requirements
  • /c del @path is the delete aspect of the command

3. Automate System Backup Using a Batch File

You can use a batch file to backup a specific folder or as part of a more substantial backup setup. You should use system backup and system restore points as part of your regular system maintenance. Sometimes, it pays to make a couple of copies of anything that might make you cry if it were deleted or destroyed.

There are many different batch file backup methods you can use. Below are instructions for a basic backup batch file and another slightly more advanced version.

Batch File Backup Automation: Method #1

backup test batch file example

Open Notepad, then input the following commands:

          @echo            off
ROBOCOPY C:\your\filename\goes\here C:\your\backup\location\goes\here /LOG:backuplog.txt
pause

Now, head to File > Save As, name the file systembackup.bat, and complete the Save.

The easy backup method works best for backing up individual folders, but isn't entirely practical for anything more complex. The batch file commands used here are:

Batch File Backup Automation: Method #2

This time you will build a longer string of folders to backup, including your system registry and other important folders.

          @echo            off
:: variables
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 ### Backing up Favorites...
%backupcmd% "%USERPROFILE%\Favorites" "%drive%\Favorites"
echo ### Backing 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 email and contacts (MS Outlook)...
%backupcmd% "%USERPROFILE%\Local Settings\Application Data\Microsoft\Outlook" "%drive%\Outlook"
echo ### Backing up the Registry...
if not exist "%drive%\Registry" mkdir "%drive%\Registry"
if exist "%drive%\Registry\regbackup.reg" del "%drive%\Registry\regbackup.reg"
regedit /e "%drive%\Registry\regbackup.reg"
echo Backup Complete!
@pause

Here's an explanation as to what the commands in this batch file mean and the bits you can customize.

First, set the location you want to copy the files to using set drive=X:\Backup. In the example, the drive is set to "X." You should change this letter to whatever your external backup drive letter is.

The next command sets the specific backup copy type your batch file will use, in this case, xcopy. Following the xcopy command is a string of parameters that include extra tasks:

  • /s copies system files
  • /c carries out the command specified by the string, then terminates
  • /d enables drive and directory changes
  • /e copies empty directories
  • /h copies hidden files
  • /i if 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 confirming you want to overwrite read only files

Now, if you want to add more backup locations to the batch file, use the following command:

          %backupcmd%            "...source directory..."            "%drive%\...destination dir..."                  

The batch file includes several folders to copy. You might note that the folders comprise different parts of your Windows user profile. You can backup the entire folder using the following command, assuming you're using the same "set drive" and "set backupcmd."

          %backupcmd%            "%USERPROFILE%"            "%drive%\%UserName% - profile"                  

Batch File Backup Automation: Method #3

The final batch file backup automation script is super simple. It involves creating a backup of a folder to an external drive, then shutting the computer down on completion.

In a new text file, input the following commands:

          Robocopy            "C:\your\folder"            "X:\your\backup\folder"            /MIR
Shutdown -s -t 30

Save the batch file, remembering to switch 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 /mir parameter makes sure that every folder and subfolder copies, too.
  • Shutdown -s -t: The shutdown command tells Windows you want to shutdown, while -s confirms it is a full shutdown (rather than a restart or entering hibernation mode). The -t parameter allows you to set a specific length of time before the system begins the shutdown process, defined in seconds. In the example, the timer is set for 30s, you can change it to whatever you like. Removing the timer parameter will cause the shutdown process to start immediately.

When you run the batch file, it will take a backup of the defined files and folders and then shut down your computer.

4. Change Your IP Address Using a Batch File

Most of the time, your computer uses a dynamic IP address to connect to the internet. Sometimes, you might use a static IP address instead, for instance, in your workplace, school, or otherwise. Sure, you could change between a dynamic and static IP address manually. But if it is somewhere you visit regularly, why not make a batch file to do the work for you?

Here's how you make a batch file to switch to a static IP address and another to switch back to dynamic:

Batch File to Switch to Static IP Address

Open a new text file, then copy in the following command:

          netsh interface ip            set            address            "LAN"            static            "xxx.xxx.xxx.xxx"            "xxx.xxx.xxx.x"            "xxx.xxx.xxx.x"                  

Where the first series of "x's" is your required static IP, the second is the network/subnet mask, and the third is your default gateway.

Batch File to Switch to Dynamic IP Address

When you want to switch back to a dynamic IP address, you can use this batch file.

Open a next text file, then copy in the following command:

          netsh int ip            set            address name =            "LAN"            source            = dhcp        

If you have more than one network you connect to regularly, duplicate the first file, and edit the details accordingly.

5. Make Your Kids Go to Bed With a Batch File

My kids aren't old enough to be playing video games in the middle of the night, but I remember my tactics against my parents so I could play Championship Manager 2 into the small hours of the morning. Luckily, my parents didn't know about using commands to control my actions.

You can use the following batch file to set a warning and begin a countdown timer on your kid's machine:

          @echo            off
:W
If %time%==23:30:00.00 goto :X
:X
shutdown.exe /s /f/ t/ 120 /c "GO TO BED RIGHT NOW!!!"

Here, the computer continually checks to see if the time is half-past eleven. When the time correlates, the message "GO TO BED RIGHT NOW!!!" will display, along with the 120s countdown timer. The 120s should be enough time to save whatever game they are playing, or their work, before the computer shuts down.

To stop the countdown, press Windows Key + R. (Of course, don't tell the kids this!)

6. Batch Rename and Mass Delete Files

I've written a more extensive article dealing with batch file renaming and deletion, so I won't explore this one too much, but you can use batch files to automate these sometimes tedious tasks. Check out the article for some extended batch commands, and get bulk deleting straight away.

7. Play Pokémon in a Batch File

This batch file has nothing to do with productivity. In fact, it's the absolute opposite. If you're susceptible to Pokémon-related gaming addictions, you should give this one a miss because it's essentially Pokémon Red in text form.

pokemon batch file text game

If you don't want to miss out, you can grab PokéBatch and start playing. Download the text file, then switch the file extension from .txt to .bat, and you're good to go.

If you like a challenge, why not check out the most fun Pokémon challenges to prove your mastery of 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'll be able to accomplish unheralded amounts of activities on your system between batch files and the Command Prompt.