Tuesday, July 30, 2013

Hidden escape character in SETX

Shamelessly stolen but very useful

I recently discovered that set and setx behave completely different when it comes to escape characters.

set bob="C:\Program Files (x86)\Notepad++\notepad++.exe"
setx bobx "C:\Program Files (x86)\Notepad++\notepad++.exe"

right? Seems correct, but one will work, while the other wont.
Executing %bob% will launch notepad while %bobx% will error out.

why? Well, apparently
setx bobx "C:\Program Files (x86)\Notepad++\notepad++.exe" equates to using set with no quotes - like so:
set bob=C:\Program Files (x86)\Notepad++\notepad++.exe

What we find, is that setx has it's own wonky magic, and if we wanted to escape a character, we use  \ ... which doesn't come up in documentation. Awesome, yea?

setx bobx "\"C:\Program Files (x86)\Notepad++\notepad++.exe"\" which would equate to 
..set bob="C:\Program Files (x86)\Notepad++\notepad++.exe"

now %bobx% will start notepad++

free plug for one of the 10^10 blogs that caught it - http://batcheero.blogspot.com/2008/03/setx-escape-character.html

No comments:

Post a Comment

Comments are welcomed and appreciated.