DOS/Windows command line escaping

Sun, Dec 5, 2004

Here is a Raymond-esque tidbit that I wanted to get out there.  I couldn't find anything on Google and had to figure this stuff out via trail and error.

Okay -- you think you know how to escape stuff on the DOS/Windows command line?  Probably not.  Let's start with a simple python script to show us what we've got:

C:\test>type dumpargs.py
# a simple program to dump the incoming arguments
import sys
for arg in sys.argv[1:]:
    print repr(arg)

Now lets run some tests and see what it spits out:

C:\test>dumpargs.py "hi there"
'hi there'

C:\test>dumpargs.py "hi \" there"
'hi " there'

So far so good. It looks like we know how to escape a double quote sign ("). But what about the pesky percent sign? DOS/Windows/Command/CMD uses this for variable substitution. Let's try a bunch of stuff and see what we get:

C:\test>set foo=bar

C:\test>dumpargs.py "hi there %foo%"
'hi there bar'

C:\test>dumpargs.py "hi there \%foo\%"
'hi there \\%foo\\%'

Hmmm, it looks like the backslash doesn't work. Looking around, it looks like the caret symbol (^) is the way to go. Lets try that out.

C:\test>dumpargs.py ^%foo^%
'%foo%'

C:\test>dumpargs.py "hi there ^%foo^%"
'hi there ^%foo^%'

Well, it looks like that works as long as it isn't in quotes. I also found another wacky case:

C:\test>dumpargs.py "hi there \" ^%foo^%"
'hi there " %foo%'

C:\test>dumpargs.py "hi there \" \" ^%foo^%"
'hi there " " ^%foo^%'

Very odd -- it looks like the caret does work as an escape character as long as there is an odd number of escaped double quote characters before it in the string. The parser here is obviously not a simple as one might first think. Lets try ending the string before we quote the percent sign:

C:\test>dumpargs.py "hi there "^%"foo"^%" bar"
'hi there %foo% bar'

C:\test>dumpargs.py "hi \" there "^%"foo"^%" bar"
'hi " there ^%foo^% bar'

We are getting closer, but that escaped quote character puts the parser into a wacky state. I finally did find a good solution:

C:\test>dumpargs.py "hi "^"" there "^%"foo"^%" bar"
'hi " there %foo% bar'

It turns out that you must replace the double quote (") with the following sequence: "^"". You must also quote the percent character (%) with this sequence: "^%". I think that everything else is okay inside of the double quotes. Very strange but true. I guess this is what 20 years of back compat get you.

Arizona Desert Photo 2

Sun, Dec 5, 2004

Here is another photo from the trip that I described here.

Snowy Trees above Page, AZ

This was a pretty lean day on the workshop.  This was our day to go and hike out to "the wave."  The Wave is a really cool set of sandstone rock formations buried in the north part of Coyote Buttes.  A permit is required to hike Coyote Buttes and the BLM restricts it to 10 people per day.  The problem is that on the weather had taken a turn for the worse during the night.  When we work up at 5 AM on Sunday, it was raining in page.  As we drove toward the trailhead we gained elevation and it started snowing.  Around 6 AM we needed to start driving on gravel roads.  It was still snowing pretty hard and we decided to turn back.  I was looking forward to a good hike and was disappointed that we didn't make it to the Wave.

When we got back to the motel, we picked up everyone else and went "hunting."  Off the side of the road outside of Page, as we were climbing, we stopped to photograph some fresh snow on desert trees.  This is one of the images that resulted.

Technical details: Original composition was 24 images (8x3).  I cropped out just a corner of that to make the composition as seen above.  Total images used then is probably more like 15 (5x3).  Each was shot at 1/25 sec, f/11, ISO 100 with the 24-70 f/2.8L @ 50mm.  Final image is 6000x6000 or 36 megapixel.