Subscribe to Windows IT Pro
October 09, 2006 12:00 AM

Using URLs in Batch Files

Windows IT Pro
InstantDoc ID #93483
Rating: (1)

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 type

mskb 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 type

winitpro 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 type

rfc 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 typing

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

  1. 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.
  2. Use double percent signs to escape a percent sign in the URL; use a caret and ampersand to escape an ampersand.
  3. 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.

Related Content:

ARTICLE TOOLS

Comments
  • MARK
    4 years ago
    Mar 13, 2008

    I noticed that in IE 6 that if I ran a batch file to open multiple URLs, like this:

    start http://www.msnbc.msn.com/
    start http://www.cnn.com/

    the result was that one IE window was open, displaying the last website (cnn.com in this example). I.e., it was reusing the window, even though the IE Option "Reuse windows for launching shortcuts" is not checked. But what I wanted was to open each URL in the batch file in a separate window.

    The solution (thanks to Alex) was to add a small delay between each START command. I used the UnxUtils sleep.exe, and using a 1 second delay caused this to work as desired.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.