Automate the Boring Stuff with Batch Files

Automate the Boring Stuff with Batch Files

ยท

5 min read

A few years ago I wrote a .bat file to automate the start of my day. Every morning for work I would open the same handful of programs and finally realized - this is the perfect thing to automate!

Computers were built for performing redundant tasks efficiently. Humans, not so much. We're prone to making mistakes and don't do well with repetitive tasks. Think muscle strain from making the same movement over and over and how quickly we become bored with repetition.

If you do anything on a computer more than once you can automate it by creating a small batch file (.bat extension). I'm going to show you an example that opens your browser, but you could apply this to any program of which you have access to the .exe file location.

Step 1: Create the new .bat File
Open notepad, or your favorite editor, and add the following line to the top of the file:

:: Automate My Morning

A [::] double colon symbol in a batch file denotes a comment and will not be executed. With longer files this can be really helpful for readability.

Save, Ctrl + S, the file to your desktop as something like "my-first-automation.bat"

Step 2: Add Some Functionality
Now, let's actually get into making the bat file do something. Let's pretend that every morning you open Chrome to pull up the news and to check your emails and glance at your social media accounts. We'll make all of this happen automatically so that you can keep sipping on your coffee for an extra minute or two. By creating a batch file you can get to work, double click the file to run your program and voila there are all your main tabs open and ready to go.

On the next line of the same file we want to make sure we're in the appropriate drive location. Enter this in as your C: drive (assuming your primary drive is C:) like so:

:: Automate My Morning
C:

This is an important step if you're working on a computer with multiple drives. For the average user it's probably not needed, but it will help reduce the chances of your program failing if for some reason it's pointing at the wrong drive.

Step 3: Find the Exe file for Chrome
To open Chrome automatically we need to know where the "executable" file exists. This is just a fancy way of saying Program. If you've ever come across an ".exe" file it triggers a program to open, start or run. We're going to take advantage of this in an automated fashion.

Click the "Windows" icon to open up your program tray, or hit the magnifying glass, then search for "Chrome": windows-icon.png

Assuming you have chrome installed you will see the program available in your search results: chrome-in-tray.png

Next, you need to right click on the program and hit "Open file location." This will open up a file explorer window with the "chrome.exe" file in it. What we're trying to find here is the exact path location to the executable (program) file. With file explorer open click on the little folder in the left hand side of the file path window (highlighted in green below) to select the full path to the current directory: Screenshot 2021-08-24 094228.png

Hit Ctrl + C to copy the full text path. Go back to your .bat file and below the line with the "C:" on it add "CD" which stands for "change directory" then paste the path you have copied after it. It should now look like this:

:: Automate My Morning
C:
CD C:\Program Files (x86)\Google\Chrome\Application

Your path to chrome may be slightly different than mine, but as long as you searched for it the way I showed you above you shouldn't have any issues.

Step 4: Add the START Command
Now that we've got everything set up, the program has us pointed to the correct directory, we can actually add the command that will run the chrome.exe file. In your script add the following code on the next line, like so:

:: Automate My Morning
C:
CD C:\Program Files (x86)\Google\Chrome\Application
START chrome "https://www.bbc.com/"

We've now added the line to start chrome. The START command is followed by the program name (notice it's without the .exe ending), then we give it a specific URL we want it to open. I'm just using BBC for this example. You can add whichever news source you fancy to this spot.

Save your batch file. Once you've done that you can test that it works. Go to wherever you saved the file - for me it's on my Desktop - and then double click it. By doing this you're essentially telling the program to "Run" which will then make it read through all of your commands. If successful you should see chrome open up with your specified URLs.

To get your email to popup enter the web address of whichever email provider you use in place of mail.google.com below. I'm also going to pull up Twitter just to show how easy it is to pull up more tabs.

:: Automate My Morning
C:
CD C:\Program Files (x86)\Google\Chrome\Application
START chrome "https://www.bbc.com/"
START chrome "https://mail.google.com/"
START chrome "https://www.twitter.com/"




Summary This was a really simple example of how to use batch files to create little programs to help automate tasks. There are infinite ways you can take advantage of this, here's just a few: create a Scheduled Task that will trigger this to run every morning, set another Scheduled Task to automatically clear out your downloads folder every night, email yourself the latest news or anything else that you can think of.

Have fun with it!

Did you find this article valuable?

Support Charles J by becoming a sponsor. Any amount is appreciated!

ย