Wednesday, April 25, 2012

Introduction to Programming: Batch Files

First off- thanks for reading my blog! The goal is to provide beginner to advanced-level insight on Programming on a variety of platforms. Today, we'll start with Windows batch files- one of the simplest ways to automate actions on a Windows PC.

To create a batch file- simply open up Notepad and click File -> Save As

Save as type "All Files" and change the ".txt" file extension to ".bat". This particular example will open a browser window with a number of tabs specified to our liking- so I'm going to call it "AutoTab.bat" and save it to my desktop.

This first program will use your default browser (I suggest Chrome). You can use a non-default browser as well- more on that later.

Now it's time to start the script. In the notepad window, type the following:

@echo off
start "name of website 1" "web address 1"
start "name of website 2" "web address 2"
... keep going until you have all of the sites that you want visible in your tabs
exit

So, for example, if I wanted to open up Google, Facebook, and Amazon- I would have the following script:

@echo off
start "Google" "www.google.com"
start "Facebook" "www.facebook.com"
start "Amazon" "www.amazon.com"
exit

Save the batch file. Launch it by simply double-clicking it.

Now, let's say that there is a website that does not work in your default browser (company intranet sites are often prime examples of this). You can add it to the script to launch in a separate browser simply by adding a few more lines. Suppose that for whatever reason Twitter only works in Internet Explorer. You would simply need to add the following line of code before "exit":

"program path- most of the time it's C:\Program Files\Internet Explorer\iexplore.exe" web address- in this case http://www.twitter.com

If you remember DOS- the cd\ command simply gets you to the root folder on your PC. You may not need 5 instances of this (you could need more, you likely need less), but if you have a few more than necessary- it really won't hurt your execution time to any noticeable degree. Now your program should look like this:

@echo off
start "Google" "www.google.com"
start "Facebook" "www.facebook.com"
start "Amazon" "www.amazon.com"
start "Twitter" "c:\Program Files\Internet Explorer\iexplore.exe" http://www.twitter.com
exit

If you'll notice- the program address needed to be entered prior to the website, and the format changed a bit. If you'd like, you could use this (or a separate script) to run a number of applications- just by adding similar coding to the script.

Tip- to get the address of programs on your desktop, Right-click the shortcut and copy everything but the Quotes in the "Target" field.

The following script will open Google Earth in addition to everything else previously executed:

@echo off
start "Google" "www.google.com"
start "Facebook" "www.facebook.com"
start "Amazon" "www.amazon.com"
start "Twitter" "c:\Program Files\Internet Explorer\iexplore.exe" http://www.twitter.com
start "Google Earth" "c:\Program Files\Google\Google Earth\client\googleearth.exe"
exit

And that's the official Matt's Introduction to Batch Files. Check back often for more tips and tricks!