Wednesday, March 31, 2010

"Fun" With Batch Files

Here's how to compute the root Program Files directory of the Visual Studio 2008 installation from the VS90COMNTOOLS environment variable... (for example...) Obviously: same technique may apply to other env vars and other programs.

The Line of Code
In a batch file:
for /f "usebackq delims=" %%d in (`echo "%VS90COMNTOOLS%\..\.."`) do set VS_ROOT_DIR=%%~fd

Directly in a command prompt:
for /f "usebackq delims=" %d in (`echo "%VS90COMNTOOLS%\..\.."`) do set VS_ROOT_DIR=%~fd

The only difference between the two is the doubling up of the "%%d" percents when referencing for loop variables. Don't ask why, just learn: that's the way it is. Actually, if you want to ask why and then go figure out the answer... that would be a good blog post for you to write.

The Dissection
On my machine, the command...
echo "%VS90COMNTOOLS%\..\.."

"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\\..\.."

Nice. Accurate and all, but look at all the ugliness we've produced. Doubled up backslashes, enclosing double quotes, ..s, not to mention the length.

So. That fancy "for /f" line. Let's clean up that output to remove the ugliness.

Using "for /f" with the "usebackq" option allows you to put a command inside backticks, as in `echo something`, and capture the output of that command in the for loop variable. In this dead simple example, the for loop would iterate exactly once, and the value "something" would be in the loop variable.

Using the "delims=" option allows you to split the output by delimiter characters. When you say "delims=" with the equal sign right up against the closing double quote, that means: no delimiter characters, give me the full output all at once. Much different result than "delims= " with a space in between... that one loops over the output separating by space characters, giving multiple for loop iterations based on how many spaces are in the output. You could also say "delims=\" to split at the path separator character, or "delims= \/:" to split at common date time separators.

Now that usebackq and delims are well understood, or at least explained, how does that help us get rid of the ugliness? Well... it's mostly the magical "%~fd" at the very end of our friendly line of code that makes the universe beautiful again. The usebackq/delims pain we went through was really just a way to get the string we want into a for loop variable so we can take advantage of for loop variable expansion modifiers.

for /f "usebackq delims=" %d in (`echo "%VS90COMNTOOLS%\..\.."`) do set VS_ROOT_DIR=%~fd

The "%~fd" says this: give me the value of the loop variable %d, and while you're at it, remove any enclosing double quotes (~), and resolve it to a full path, assuming the variable represents a file or directory name (f).

The net effect of all this is that our line of code...
for /f "usebackq delims=" %d in (`echo "%VS90COMNTOOLS%\..\.."`) do set VS_ROOT_DIR=%~fd

Finally, in the end, simply evaluates to the line of code we wanted to write in the first place, but without hard coding a machine specific path name in a batch file:
set VS_ROOT_DIR=C:\Program Files (x86)\Microsoft Visual Studio 9.0

Type "help for" in a Windows command prompt for more of the gory, sickening details regarding loop variable expansion.

And help keep the universe beautiful. Even if you still have to write batch files once in a while.

Thursday, March 04, 2010

Kickbox: The Workout Playlist

OK... so I'm not sure why people are asking for this sort of thing on Facebook lately, but remember... you asked for it. Lara, cousin Matt, talking to ya both. (I'd like to take credit for this, but really it all goes to my sweetie, Robin Holly. She put this list together back when she was teaching a kickboxing class in our Seattle area Tae Kwon Do dojang.)

Kickbox: The Workout Playlist
  • Mr. Bartender (It's So Easy), Sugar Ray
  • Look At You, Hanson
  • It's My Life, Bon Jovi
  • Everyday, Bon Jovi
  • Sk8er Boi, Avril Lavigne
  • Two Hearts Beat As One, U2
  • You Really Got Me, Van Halen
  • Walk This Way, Aerosmith
  • Satellite, P.O.D.
  • Bounce, Bon Jovi
  • Pour Some Sugar On Me, Def Leppard
  • Hey Baby, No Doubt
  • Elevation, U2
  • Objection Tango, Shakira
  • Send Your Love, Sting

And don't forget to cool down...
  • Little Wing, Sting
  • Valparaiso, Sting

Whew! This was gonna be the longest Facebook post ever... but then Facebook told me it was too long. I never knew there was a 420 character limit to those suckers.


Monday, March 01, 2010

Must Have Windows Software

Whenever I get a new computer running Windows, like I am today at work, the first thing I download and install on it is Firefox. Then, using Firefox, I have to download and install the following shet of shtuff to make it programmer friendly. This is my way of avoiding being ripped to shreds by the open source sharks for actually having paid money for a computer running Windows... Actually, to avoid that completely, I'd have to install emacs or gvim, too. But I won't do that until somebody else has to use the computer and looks like he's floundering without it.

Must haves:
  • Process Explorer
  • notepad++
  • PuTTY
  • VNC
  • CMake
  • TortoiseCVS, including CVSNT command line client
  • TortoiseSVN
  • SVN command line client, usually the CollabNet one
  • Git, the msys one
And then, the hard core development tool:
  • Microsoft Visual Studio, whatever version(s) necessary to do my job...
And then, as necessary, but only as necessary:
  • xampp, for a localhost web server
  • NSIS, the installer builder
  • doxygen
  • graphviz
  • ActiveState Python
  • ActiveState Perl
  • ActiveState Tcl
  • MagicDisc, or similar ISO image mounting utility

notepad++ -- like notepad, but seriously OD'd on steroids: all kinds of built-in language syntax highlighting

VNC -- if I'm lucky, one that includes a free server so I can access the Windows box from other computers... if not, then at least RealVNC client so I can access other machines

Anybody out there have any other "must haves" that belong on a Windows box?