Tuesday, July 29, 2008

Using parameter qualifiers in Windows shell scripts

Summary: How to extract file information from command-line arguments in batch (.BAT) files.

It may seem archaic, but despite the advancement of more recent Windows command shell tools and technologies -- such as Windows Script Host, PowerShell, etc. -- the original Windows NT® Shell is alive and well. VBScript, and PowerShell scripts may look young and hip, but old and proven batch (.BAT) files still offer the easiest way of performing simple operations, such as copying, moving files, or launching programs.

I have been writing batch files for many years, but I still struggle to remember a few common things such as runtime substitution of parameter qualifiers. Parameter substitution is most helpful if you want to extract parts of a file name. For example, you may want to extract directory path from the full path of the running batch script, so you can build a path for another file located in the same directory. Or maybe you want to use the folder path to access a parent directory. There are many good uses for parameter substitution, so this is how you can do it.

First, remember that command-line parameters are referenced using percent sign followed by the parameter index, such as %0 (references the running batch file), %1 (first parameter), %2 (second parameter), and so on. To extract a part of the path name from the parameter, insert tilde (~) followed by one or more of these specifiers between the percent sign and parameter index (%~fdpnxN, where N is the index of the argument):

QualifierExpands to a...
fFully qualified path name
dDrive letter and colon character
pPath (to a directory) without a drive letter, file name, or file extension (may contain a trailing backslash)
nFile name only without a drive letter, directory name, and extension
xFile extension with a leading period
The following examples show how to extract file information for the running batch script (assume that the script's full path is C:\Scripts\Test.bat):
Returns name and extension of the running batch script: Test.bat

Returns directory and folder path of the running batch script: C:\Scripts\

Returns quoted full path of the Other.bat file in the parent folder: "C:\Scripts\..\Other.bat"
Notice that all samples above use argument #0 (the running batch file), but you can use parameter substitution with other arguments as well. Also, there are other qualifiers, which can be used for parameter substitution, but I did not mention them because they are rarely needed. To learn more about parameter qualifiers in Windows shell scripts, see references below.

Additional references:
The Set, Call, and Shift statements by Richard Charrington (see The call statement section)
Windows NT® Shell Scripting by Tim Hill (the best book on the subject)

Friday, July 25, 2008

Blogging and podcasting: personal syndication for fun and profit

Summary: Presentation with links and references, which can help you find, use, and produce blogs and podcasts.

I recently gave a presentation at work which my teammates seemed to like. The presentation discusses blogging and podcasting and addresses such questions as:
  • Why would you want to read blogs and listen to podcasts?
  • Where can you find interesting blogs and podcasts?
  • How do you start a blog?
  • How dow you produce a podcast?
  • Where do you store the podcast files?
  • How do you stream audio and video podcasts?
  • And many more.
Coincidentally, at about the same time, a couple of (non-technical) friends asked me about the very same topics, so I thought I would make this presentation available online to help both technical and non-technical -- okay, make it semi-technical -- people, who are just starting on the path to blogging and podcasting.

Here is the inline version of the presentation (the best part of the presentation is links to resources and articles, so don't miss them):

If you want to access the presentation directly, use the following links:By the way, I was not sure which presentation sharing/hosting service to use, so I started investigating by reading two excellent reviews at Digital Inspiration:First, I tried the services not mentioned in the reviews: ThinkFree's Online Office and Microsoft's Office Live Workspace. I did not like either of them. ThinkFree removed all hyperlinks from the presentation and did not offer any way to put them back, while Office Live's sharing screen looked rather ugly (dunno, maybe I did not use it correctly). Anyway, I tried Scribd recommended by Digital Inspiration, and I really liked it. I suspect that there may be better options (such as SlideShare), but Scribd fulfilled my immediate needs; I may try other services in future, though.

UPDATE #1: Apparently, Scribd turned off hyperlinking capability from the online presentations, which is a shame. I'm now back to square one: looking for a better online presentation hosting service.

UPDATE #2: I just found out that Docstoc offers pretty much the same features as Scribd, but it also supports embedded hyperlinks. Kudos to Docstoc. I updated the embedded presentation (and reference to it) to point to the Docstoc version.

Wednesday, July 23, 2008

On scripting

Summary: Scripting resources for Windows application developers and administrators.

I suspect that experienced Windows system administrators are familiar with these tools and resources, but if you are an occasional script writer and never heard of these, check them out:
  • The Portable Script Center is a self-contained help (CHM) file containing dozens of sample scripts for managing Active Directory, applications, desktop, Group Policy, hardware, IIS, and a much more.
  • Scriptomatic is a utility that can help you write WMI scripts for system administration. It generates WMI scripts in VBScript, Perl, Python, and Jscript. Scriptomatic supports numerous output formats from plain text to Web page to XML.
  • WMI Code Creator generates VBScript, C#, and VB.NET code that uses WMI to perform management tasks such as querying for data, executing a method from a WMI class, or receiving event notifications.
  • ADSI Scriptomatic generates scripts to manage objects in Active Directory.
  • Tweakomatic generates scripts to manage Windows and Internet Explorer settings.
More tools and resources are available at Microsoft TechNet Script Center.

Friday, July 18, 2008

Database installer revised

Summary: Overview of changes made to the previously published database installer script (dbsetup.vbs).

I'm not sure what happened to the new and "improved" MSDN Magazine (the web site), but all of a sudden, I started getting messages from readers asking to send them the VBScript file (dbsetup.vbs) accompanying my Data Deployment: Streamline Your Database Setup Process with a Custom Installer article (from the September 2004 issue of the magazine). I checked the site and did not find a download link, so I'm offering* the most recent version of the script here:

I'd also like to mention a couple of alternatives which you may like better than my approach, but first, let me describe modifications I made to the script in case you insist on using it. (If you don't know how the script works, please read the article; it explains the script execution logic.)

The most recent update to dbsetup.vbs was triggered by an idiosyncrasy in Visual Studio 2008. For some reason, Visual Studio 2008 saves new SQL source files added to database projects in the UTF-8 format, even if the files only contain regular 7-bit ASCII characters. [I could not find an explanation to this phenomenon, but since new files added to other (non-database) projects get saved in the ASCII format, I suspect that the issue is caused by the default file templates associated with the Visual Studio 8 database projects (someone at Microsoft must have accidentally saved these files encoded in UTF-8). I may be wrong, though.]

UTF-8 encoded files containing English (ASCII) text are almost identical to regular ASCII files, except that the former include three special bytes called byte-order mark (BOM) identifying the encoding format in the beginning of the file. Most programs, such as text editors, can handle UTF-8 encoding (you may not even notice that the file is UTF-8 encoded), but unfortunately, VBScript (the scripting engine) is not one of them.

When opening a text file in VBScript, you can specify whether the file is encoded in ASCII (TristateFalse) or Unicode (TristateTrue), but if you specify the Unicode format, VBScript will assume that the file is encoded as UTF-16, which is not the same as UTF-8. Because VBScript did not (and still does not) support the UTF-8 format, the original version of dbsetup.vbs could not process UTF-8 formatted files (the returned database error complained about invalid characters).

To handle UTF-8 encoded files, I added a workaround: when processing text files (SQL scripts and others), dbsetup.vbs now looks for the Unicode byte-order marks. If the script finds the UTF-8 specific bytes, it discards them and processes the rest of the file as regular ASCII text (Note: If you want to include real Unicode characters in text files, use UTF-16 encoding, which VBScript and dbsetup.vbs can handle). This workaround is not very elegant, but it seems to work.

Two other modifications affect repair scripts (scripts executed to repair a database without incrementing its version). To provide support for multiple database versions (in source control), dbsetup.vbs now expects to find repair scripts under the version labels in the Repair folder (the Repair folder is expected to have subfolders named after the database versions to which they apply, similar to the approach used for grouping the upgrade scripts). Also, to make sure that repair scripts added during test cycles get executed during production deployment, dbsetup.vbs will process them after an upgrade (only repair scripts defined for the current version will be processed).

Finally, dbsetup.vbs now supports conditional execution of scripts. If you want to selectively include certain scripts, keep them in a subfolder (under a regular folder) named after a directive you'll use when running setup and/or enclose the file references in the #if DIRECTIVE ... #endif code block (replace DIRECTIVE with a meaningful name) if you explicitly name the scripts in of the Files.txt files. To include the script, launch dbsetup.vbs with the /set command-line parameter followed by a directive you want to include, such as /set:DIRECTIVE. When dbsetup processes a folder, it first handles regular scripts, and then, if launched with the /set command, it will process all conditional files from the subfolders matching the directives included in the /set command, or the conditional sections of the Files.txt file.

I have to say that there may be better database deployment methods than my simple process. For example, you can try using Database Schema Build & Deployment Tools offered by Database Edition and Team Suite of Visual Studio Team System 2008.

Tip: In addition to Microsoft, other third-party vendors started offering database installation tools, such as:

A couple of weeks ago, I read an article Deploying Database Developments** in which Alexander Karmanov describes an alternative database deployment methodology. Although Alexander's approach seems a bit more complicated (then, say, my technique), it may offer a more comprehensive solution. I didn't have much time to delve into Alexander's solution, but you may want to give it a try.

Additional references:
Get Your Database Under Version Control by Jeff Atwood
Evolutionary Database Design by Martin Fowler and Pramod Sadalage
11 Tools for Database Versioning by secretGeek

*I wrote dbsetup.vbs in a rush a few years ago, so now, when I'm looking at the source code, I can see how imperfect it is. Frankly, I'm a bit ashamed that this code has been made public, but I also do not want to waste time rewriting the parts that work. Anyway, if you are eager to criticize the programming style and approach, I'm with you.
**Does the title Deploying Database Developments sound right to you? Development is a process, so how do you deploy a process? You can deploy an application, code, etc, but development... I don't think so.

Saturday, July 12, 2008

Technobrief #3

Summary: Recent discoveries: software, articles, videos, quotes, humor, and more.

Since the last technobrief, I have added the following to the list of items worth checking out:

  • From A progressive view of IT by Bob Lewis:
    ManagementSpeak: We just upgraded to a new version of the software.
    Translation: Not only does the software not work the way you expect it to, it doesn't work at all.
  • Tablefy tool for creating and publishing comparison charts of any items.
  • Tips for New Bloggers offers great tips for Blogspot (Blogger) users.
  • From Why I Still Use Windows Despite the Peer Pressure by Adam Frucci:
    "And you know what? Macs are too hip. Oh, look at me! I do graphic design! I wear women's jeans and hang out in coffee shops! I'm a DJ! Well good for you. My computer is not a fashion statement. It's a computer."
  • From The Road to FogBugz 4.0: Part I by Joel Spolsky:
    "RFP stands for "Request for Proposal." It's a request by a large company for a custom proposal from a small company. The small company works on the 200 page laser-printed proposal like mad for three weeks and Fedexes it in great expense and at the last minute, where it gets put in the trash because the large company has their favorite vendor who takes them on a helicopter to Atlantic City on junkets involving blackjack and strippers, and who is going to get the contract no matter what, but someone in purchasing for some unexplained reason, maybe he's bucking for a promotion is insisting that the proposal be opened up to "competitive bidding" and the small company has been chosen as a victim to write up a proposal that has no chance of being accepted just to make the process look a little bit less corrupt, and if you're a small company, I would recommend that you don't fall for it and don't spend any time responding to RFPs unless it's already understood that you're going to get the contract."
  • From Done, and Gets Things Smart by Steve Yegge:
    "All too often I find myself on interview loops where the candidate knows a seemingly astounding amount about coding, computer science, software engineering, and general industry topics, but we find out at the last minute that they can't code Hello, World in any language."
  • From How Do You Find the Best Employees for Your Company? by Dare Obasanjo:
    "The real barrier is that most employers [...] expect people to want to work for them which means they'll get a flood of awful resumes, put a bunch of candidates through the flawed interview process only to eventually get tired of the entire charade and finally hire the first warm body to show up after they reach their breaking point."
  • From Acting Squirrelly: If SAP could make R/3 easier to use they would do it, right? by Robert X. Cringely:
    "SAP and companies like it do something similar by making powerful software that is quite deliberately difficult to use. They could make it easier. Heck, the capability to make it easier is shipped right with the software, though never pointed out to the customer. I used to think this was a matter of geek machismo, where higher value was placed on processes that were more difficult to command simply because it could be used to maintain for the techies an upper hand against management. But now I think it's much simpler than that and SAP just wants its software to be more difficult to use because that maximizes revenue."
  • From a 2003 e-mail rant by Bill Gates:
    "Microsoft.com is a terrible website."

Thursday, July 10, 2008

How to watch laptop on TV

Summary: Streaming videŠ¾s from your laptop to a TV set is easier than you may think.

Why would you want to hook up a laptop to a TV? No, watching YouTube videos on a big screen is not the best idea (the resolution of YouTube videos is too low for TV), but there are better reasons.

First, many TV shows are now available online in reasonable resolutions. You can stream such programs as Frontline and 60 Minutes (two of my favorites), along with many offerings from PBS, CBS, and other broadcasters. I bet you will enjoy watching your favorite show on the TV in the living room more than on the laptop in the office.

Say you are a proud TiVo owner, so catching up a missed episode of your favorite TV show is not your problem. But what about other video content, such as Netflix or Amazon Unbox video downloads? Wouldn't you prefer watching these on a laptop or a TV?

Or, maybe you borrow a movie on a DVD from a friend for a day, but do not have a chance to watch it. Instead of making a personal copy of the DVD (which is obviously illegal), you can rip it to an ISO file on your laptop's hard drive (you can use free DVD Decrypter, DVDFab HD Decrypter, or DVD Shrink for ripping DVDs). When you have time to watch the DVD, mount the ISO file to a virtual DVD drive and watch it as a regular DVD (for mounting ISO images, use free Daemon Tools or MagicDisc). When you finish the movie, unmount the virtual drive, and delete the ISO file. [While making unauthorized copies of copyrighted media is prohibited by law, I'm not sure if an ISO image, which has not been burnt to a DVD, can be considered a "copy", but law professionals may disagree. I'm rather sure that RIAA's lawyers would consider this a crime, but I doubt that they would pursue prosecution of such "offenses". Don't take my words, though, and use your own judgment to consider the risks.]

In case you wonder what the quality of the streamed video you should expect, it will largely depend on the quality of the video source. Regular YouTube videos will look awful on TV, but video content intended for viewing in the full-screen mode, such as PBS programs and ripped DVDs, should be pretty decent, although HDTV purists will probably be disappointed.

To connect your laptop to a TV, you need to get a proper cable (or cables). Regardless of the type of cable, it must be long enough. If you intend to place your laptop on the coffee table in the living room, make sure that the cable reaches from the laptop to the TV (Hint: a standard 6 feet A/V cables from a 99¢ Only store will probably be too short).

If you own a high-end (expensive) laptop and TV set, check if they have High-Definition Multimedia Interface (HDMI) ports; if so, you can connect them via an HDMI cable.

HDMI cables combine both video and audio signals, so you'll need only one cable. They support both high-definition and regular video. Normally, HDMI cables are expensive, but at the time of writing I found a 15 feet HDMI cable for $6 (plus shipping and handling) offered by MonoPrice*; a better quality cable - which you may or may not appreciate - can cost significantly more ($20-$40). If you decide to buy an HDMI cable, make sure you pick the right type (extra length may affect the signal strength in an HDMI cable, but again you may or may not notice this).

If you cannot use an HDMI cable, check if your laptop has an S-Video (also known as Y/C) port. Most laptops and TV sets support S-Video, so this is your most likely option.

S-Video connectors can have different number of pins, but 4-pin connectors are the most common; nevertheless, check your laptop's and TV set's S-Video sockets to make sure that you get the right connector (you will not be able to plug a 7-pin S-Video connector into a 4-pin S-Video port).

S-Video cables only transfer video signals; to pass audio from the laptop to the TV set, you will need to use a standard 3.5mm stereo to two RCA audio cable, such as this one.

You plug the 3.5mm jack on one side of the cable into the laptop's headset port and the two (red and white) RCA connectors on the other side of the cable into the TV set's RCA audio input sockets (make sure you do not get the cable with two RCA plugs on both ends by mistake). If you can, get both the S-Video and RCA audio cables of the same length.

If you do not want to mess with two different cables, check out an S-Video|3.5mm to composite|stereo combo cable, which connects laptop's S-Video and 3.5mm audio ports to the three RCA A/V sockets; this cable has the S-Video and 3.5mm stereo jacks on one side and three RCA plugs on another side (see example at MonoPrice).

If neither of the above-mentioned options work for your laptop and TV set, you should be able to connect them using an adapter (there are lots of audio and video adapters out there). A new service, WireWize (currently in beta), may help you figure out how to connect your devices. [I could not test WireWize, because it did not find the model of my TV, so your mileage may vary.]

Once you connect your laptop to the TV, you can switch video output from laptop's LCD to the TV. Most newer laptops will detect connection to an external display (such as a TV set) and show you a prompt to switch automatically (you can project output to TV only, or keep it displayed both on the laptop and TV). You can also change the display output settings via the system display properties or function key (such as Fn+F4, Fn+F5, or some other keys, depending on the laptop make and model).

In addition to changing laptop's video output display settings, you must also adjust the source of TV input (using TV's remote). Depending on the type of connection, it can be Video 2, HDMI 1, or something else (just flip through the options until you see the laptop's display).

TIP: You may find it handy to turn your laptop's screen saver off temporarily, while you are streaming video to the TV; otherwise, you will have to move mouse or press a key every time the screen saver kicks in. You can do it manually, but it will be easier if you use a free program Caffeine. After you stop video streaming, just stop Caffeine from the system tray.
If you're still not clear how to connect your laptop to TV, check the references below or watch the following short video:

See also:
How To Connect Your Laptop To Your Television
How to Connect a Laptop Computer To Your Television Screen
How to connect your Laptop/PC/Computer to your TV
How-to: Connect your PC to your television and stereo.
How can I connect my laptop to TV?
How to Connect a Laptop Computer To Your Television Screen
How to Change the Primary Monitor on a Laptop
How To Connect A Laptop To A TV

*Be careful: when streaming video, InTV displays SMS messages at the bottom of the screen which often contain obscenities (I could not find any way to block them).
**This and other links to hardware vendors in this article are not paid advertisements. I reference them because I purchased or intended to purchase the same or similar products from these vendors and found the prices, charges, and services provided by these vendors reasonable.