It’s the mundane that makes the day drag on. Simple, repetitive tasks that sap your life force. We either do them begrudgingly, or we neglect them until they create an even larger problem. It doesn’t have to be that way!
With Windows computers, you can create simple little programs, called batch files , that will take care of these tiresome tasks.
That’s the point of automation, isn’t it? To set it and forget it, as the infomercials say. Let’s take a look at the parts of a batch file and how to create some cantrips for your chores.
What’s a Batch File?
Back before Windows, everything was accomplished by typing commands instead of pointing and clicking. The Microsoft version is known as MS-DOS (Microsoft Disk Operating System) and the commands are known as DOS commands .
Sometimes, there were tasks where all you needed to do was enter commands in the same order, every time. You could create a file of these commands and name it something like BORING_TASKS.BAT. Then, by simply typing BORING_TASKS.BAT and pressing the Enter key, that list of commands would be executed.
As Windows came on the scene, Microsoft was still depending on these batch files to do things in the background. They had to leave the capability to run these files. That maintained the ability for people to create and run their own batch files.
Batch files often became the go to tools for power users and system administrators alike. There’s no reason why you can’t use batch files, too! Let’s look at a few things you could do.
How to Create a Batch File
All you need is Notepad, a cheat sheet of batch commands, and a little time and patience. Open up Notepad, write the commands you want, then save the file, but change the Save as type: field to All Files, and change the file extension to .bat. That’s really important. Now you’ll have a batch file that you can use as you see fit.
If you need to make changes to the batch file, right-click on it and choose Edit. This will open it up in Notepad and you can edit it again. Simple.
To master the basics, go through our How To Write A Simple Batch File article. Let’s look at a few tasks we could handle with batch files.
Limit Computer Time with a Batch File
Windows Family Safety has been around since Windows 7 and can handle the job of limiting computer time quite nicely. But maybe you want to set up the computer so that no matter who logs on, they’ll only get a certain amount of time on the computer before it shuts down. Or maybe you want to use your computer to stream radio, but shut down after a while in case you leave or fall asleep. There’s a batch file for that!
@echo off shutdown -s -t 3600
@echo off – Tells the computer to not show anything that comes after it in the command window when the batch file runs.
shutdown – This command is obvious, but the parameters -s and -t aren’t necessarily as obvious. -s tells the computer to really shut down, not to just log off (-l) or reboot (-r). The -t parameter is what tells the computer to use a timer.
Once the batch file is initiated, the computer will start counting the seconds. For this example, the timer runs for an hour, which is 3600 seconds. When the time hits 3600 seconds, the computer will shut down. You can change that number to anything you want.
If you want to limit how long a computer runs after someone logs in, save your shutdown.bat file to the Startup directory. In Windows 7 and earlier it will be located at C:\Documents and Settings\All Users\Start Menu\Programs\Startup. In Windows 8, and 8.1, it’s located at C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp.
Delete All the Files in a Folder
If you have a program that creates lots of log files in a specific directory, it can be time-consuming and annoying to try to delete them all the old fashioned way. Let’s create a batch file that deletes all the files in a folder, without you even having to think about it. The code below is very simple and only empties one specific folder.
cd C:\Folder\Subfolder del /f/q *
cd – Tells your computer to change directory to the location you’ve specified. In this example it’s C:\Folder\Subfolder. It will delete ALL files inside of that folder. Change that to whatever you wish, but make sure it’s really the directory you want to empty, for example a download folder.
del – This means delete. The /f parameter forces the deletion of read-only files and the /q parameter tells the computer to do it quietly. That means that the names of all the files you’re deleting won’t print out in the command window. The asterisk (*) tells the computer to delete ALL files. The asterisk is a wildcard character.
You can get more precise with what you want to delete. Let’s say that you only want to delete all the plain text files in a directory, because those are the logs that are taking up space. Instead of having the asterisk at the end of your del command, you could use *.txt. That tells it to delete any file with the file extension of .txt. Now, only those files will get deleted from that directory.
cd C:\Folder\SubfolderOne del /f/q *.txt
Let’s say there’s lots of text files in there, and all your log files begin with log-dd-mm-yy. Simply use the asterisk between log and .txt, so it will look like log*.txt. That will delete any file whose name begins with log and has the file extension of .txt.
cd C:\Folder\SubfolderTwo del /f/q log*.txt
Clear out multiple folders? Simple. Add another section that is the same, but have your cd command point to the other folder. Then your batch file looks something like:
cd C:\Folder\SubfolderOne del /f/q log*.txt cd C:\Folder\SubfolderTwo del /f/q log*.txt
Play with this batch file and you’ll find many creative ways to use it. You can have it as a batch file that you manually run, or you can use Windows Task Scheduler to make it run at a set time every so often. You could even have it run on startup as a lightweight alternative to CCleaner .
Choose a Random Number
Here’s a nice little batch file that will generate a random 4-digit number. Why would you want to do that? Lots of things require 4-digit pass codes, like the PIN for your bank card for example. Maybe you manage users in a business and your access control system uses a 4-digit number. Many of them do. If you just try to pull one out of your head, you’ll eventually find yourself creating pass codes that are anything but random. This batch file will take care of that.
@echo off :start set /a ran=%random% if /i %ran% GTR 9999 goto :start if /i %ran% LSS 1000 goto :start echo Batch chose the number %ran% pause
:start – Creates a label named start, that acts like a rally point. Anytime a goto command points to it, the computer will go back to that point in the script and do whatever it says on the line after that label.
set /a – Tells the computer that there’s going to be a new variable that’s a number, the /a parameter tells it to round down to the nearest whole number. So if the value was 1234.6, it would get rounded off to just 1234.
The name of the variable is ran. You could call it whatever you want, but ran makes some sense for a random number.
%random% – This is a dynamic variable, meaning each time the computer comes across it, it’s going to assign it a new value. The %random% dynamic variable will generate a number from 0 to 32767.
We want a 4-digit number so it has to be between 1000 and 9999. That’s what the next 2 lines are for.
if – It’s the beginning of a conditional statement – IF this condition is true THEN do something. The first line is saying if the integer in the variable ran is greater than (GTR) 9999, go to the start and try again. If the value of ran is lower than (LSS) 9999, the computer will continue to the next line. The second line is saying if the value of ran is less than 1000, go to the start and try again. There are several different operators we can use to compare numbers in a batch file.
You might be thinking that 0001 is a 4-digit number, and you’d be right. Only, the %random% dynamic variable won’t generate that. It could generate the single digit of 1, but it won’t put the 3 leading zeroes on it. That limits us to just 8999 4-digit numbers. Surely that’s enough for our purposes.
Eventually, the computer will put out a number that falls between 1000 and 9999. Let’s say it picked 4428. At that point, the batch file will use the echo command to display the sentence, “Batch chose the number 4428”. You’ve got your 4-digit random number, now. This process goes a lot faster than trying to explain it–well, less than a second.
pause – It holds the command window open until you press a key. That gives you time to write down your random number.
Rename Several Files with a Batch File
Files downloaded with BitTorrent often have a lot of excess letters in their names. There’s nothing wrong with that, yet it would look nice if you could remove all that extra stuff. Let’s say you’ve downloaded public domain eBooks and the file names all look like BookName.UploaderGuy.PD.epub, where BookName is the name of the book. You can use this batch file to strip that .UploaderGuy.PD out of the file name.
@echo off SETLOCAL ENABLEDELAYEDEXPANSION SET old=.UploaderGuy.PD SET new= for /f "tokens=*" %%f in ('dir /b *.epub') do ( SET newname=%%f SET newname=!newname:%old%=%new%! move "%%f" "!newname!")
There’s a lot going on here. Some of it will make sense based on what we’ve learned so far. This is a good point to send you off on your own to expand on what we’ve covered and to start teaching yourself more about the power of a well crafted batch file. Let’s call it independent study time.
End of File
We’ve covered what a batch file is, the basics of creating one, and a few examples of useful ones. We’ve shown you where to find more information about creating and using batch files. Hopefully, we’ve also had some fun along the way. Happy batch filing!
Got a favorite batch file utility you’d like to share? Questions about how to customize these scripts to your needs? Know of other good batch command resources? Share them in the comments. That’s where we can all learn and help each other. After all, we’re all in this together.
Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.