Batch files provide advantages over using desktop shortcuts or browser favorites to connect to frequently used Web sites. You can launch sites without needing to hunt down the page in your favorites list and even use command-line arguments to retrieve specific content much more quickly. If you put the batch files in a common tools directory on the network, you can access them from any machine, even if you’re logged on as another user. However, the command shell’s treatment of certain common URL characters, such as the percent sign (%), can make batch files containing URLs fail, even though the commands you use will work fine if you type or paste them at a command prompt. When you understand why this problem occurs, you can work around it, and I’ll show you how by using a couple of Google and Microsoft Knowledge Base lookups as examples.
Performing a Google Phrase Search
The situations in which I usually see this problem involve extremely long Internet URLs, frequently for reference pages in online specialty sites; however, the problems are the same as you’d encounter with a simple Google phrase search. For example, if you look up the phrase parent process on Google, the URL at the top of the results page will be http://www.google.com/search?q=%22parent+process%22. The Windows command shell can’t understand commands that begin with protocol handlers such as “http://”, but if you invoke the URL with cmd.exe’s Start command, the URL will automatically be passed to the default Web browser. For example, entering the following text at a command promptStart http://www.google.com/ search?q=%22parent+ process%22
opens your Web browser and passes the page request to Google. (Although this command appears on several lines here, you would enter it on one line in the command-shell window. The same holds true for the other multiline commands in this article.) So far, so good.
Now save this command line in a batch file—named, for example, google1.cmd—and run it at a command prompt. The Web browser will open a Google page, but there probably won’t be any results for the search, and the browser’s address bar will contain a slightly different URL: http://www.google.com/search?q=2parent+process2. When you paste this line into a batch file (call it google1.cmd) and run the file, the search will fail.
What Went Wrong?
The problem is that the command shell reserves the percent sign as a shell variable delimiter. When cmd.exe reads a batch file, it expands the shell variables in the batch file before executing it. Although standard shell variables both start and end with a percent sign, some expansion parameters also begin with a percent sign. One group of these expansion parameters is the percent sign followed by a number: The shell uses %0 through %9 to refer to the elements of the command line that launched a batch file. The %0 always refers to the first element, the name of the batch file, and %1 through %9 represent the next nine elements on your command line. If you didn’t have any command-line arguments, cmd.exe simply replaces them with empty strings. If you rerun google1.cmd with the command linegoogle1 argA argB
the browser displays the URL http://www.google.com/search?q=arg B2parent+processargB2 because the shell transforms each %2 into argB.
This glitch has a simple solution. If you need to use a literal percent sign in a batch file, you must escape it by doubling it. When the command shell sees double percent signs (%%), it replaces the double percent signs with a single percent sign. The google1 .cmd batch file will work fine if you change it to the following:start http://www.google.com/ search?q=%%22parent+ process%%22
Let’s expand the problem a bit further. Google lets you specify the number of results to return on each search page. To return 100 results per page, you append &num100 to the URL. (This number can be any number from 1 to 100; if you request more than 100 pages, Google will still return results but will cap the quantity at 100.)
Unfortunately, if we add &num =100 to the URL in the batch file, it won’t show up in the Web browser URL. Instead, the command prompt window in which you invoked google1.cmd will display the error message “‘num’ is not recognized as an internal or external command, operable program, or batch file.”
The problem here is that the ampersand (&) character is a special symbol for the command shell as well. By itself, an ampersand ends a statement, allowing you to enter another shell command on the same physical line.
To get around this problem, you need to use the command shell’s generic escape operator, the caret (^) symbol, to escape the ampersand characters in batch file?based URLs. Using the Google example again, I usually set my Google searches to return 100 results at a time. When I do that, the parent process search displays this URL in my browser: http://www.google.com/search?q=%22parent+process%22&num=100. To add &num100 to the URL in a batch file, I use the following syntax:Start http://www.google.com/ search?q=%%22parent+ process%%22^&num=100
Making Variable Substitution Work for You
The variable substitution process might force you to use escape sequences for symbols in hard-coded URLs, but it also presents you with an opportunity for flexible scripting: The substitution process means you can write simple one-liner batch files that use command-line arguments. This capability has been a real time-saver for me. I typically look up Microsoft Knowledge Base articles several times a week—a process that’s extremely annoying via the GUI when you know exactly what you’re looking for.
I start my browser, navigate to http://support.microsoft.com, and click Advanced Search. I select Article ID as the search type and enter 123456 to find Knowledge Base article 123456. This returns a search result set containing one item that I then need to click once more before I get to the article page.
For Web sites on which I need to look up information regularly, I have a handful of one-liner batch files that do the work for me via command-line variable substitution. When content providers modify their Web site structures, I occasionally need to modify the files—and these examples may go stale for that reason—but updating the batch files takes only a few seconds, and they easily save me a couple of hours of repetitive browser navigation every month.
In the example below, I’ve added one element: I begin each command line with the at (@) symbol to suppress echoing the original command. The batch file I use to look up articles in the Knowledge Base, mskb.cmd, contains the following single line:@start http://support.microsoft.com/ kb/%1
When I want to see Knowledge Base article 830473, I simply typemskb 830473
and the command shell expands this to http://support.microsoft.com/kb/ 830473.
I’ve created similar batch files for finding particular content on several other Web sites that I use regularly. Here are a few examples. Note that over time, specific examples might stop working because organizations change Web site structures, but you can update them or come up with your own by looking at the URL after navigating your browser to a page manually.
To look up a Windows IT Pro article-if I know its InstantDoc ID, I use a batch file named winitpro.cmd that contains the following command:@start http://www.windowsitpro.com/Articles/Print.cfm?ArticleID=%1
To find article 7992 I just typewinitpro 7992
and the command shell changes the %1 to 7992. If I want to find a specific Request for Comments (RFC) document, I use the following as the content of a file rfc.cmd:@start http://www.ietf.org/rfc/ rfc%1.txt
To see RFC 821, I just typerfc 821
and the command shell replaces the %1with 821. To look up Microsoft security bulletins, I use this content in the file mssb.cmd:@start http://www.microsoft.com/ technet/security/bulletin/ MS%1.asp
Then I can look up bulletin 06-001 by typingmssb 06-001
By the way, this process works fine even if the Web site requires authentication. You’re invoking the Web browser interactively, so if your credentials are cached by the browser or if the Web site automatically redirects you after you manually authenticate, you get to your target Web page.
Creating Your Own URL Wrapper Batch Files
Once you understand how the command shell treats URLs, you can write batch file wrappers for your own Web site lookups with little effort. Just remember these three points:
- Use @start as the command to invoke the URL. The @ symbol suppresses the command shell’s automatic command echo; start passes the URL to the shell so that all standard protocols (e.g., http, https, ftp) invoke the default Web browser.
- Use double percent signs to escape a percent sign in the URL; use a caret and ampersand to escape an ampersand.
- To make the batch file insert a command-line parameter as part of the URL, simply insert %1into the URL where you want the data.You can extend this to multiple parameters if necessary.