#1 01 Dec 2005 14:41

ajw
Member
Registered: 01 Dec 2005
Posts: 2

cmd.exe /c and pathnames with special chars

On Windows XP, I have this file: "C:\(ba&tch).bat" for argument's sake.

Executing it via the cmd /c <pathname> mechanism requires escaping the special characters:-
cmd /c "C:\^(ba^&tch^).bat"

The weird thing is that if you have a space anywhere in the path (including in a subfolder name if the file is stored deeper that the root of C) then execution will not work.  Is there a way around this?  (You can simply execute the file on its own and the file association mechanism will run it correctly with Cmd.exe, but I wonder if there's a way to do it via cmd /c).

Thanks.

andrew

Offline

#2 01 Dec 2005 20:14

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,031
Website

Re: cmd.exe /c and pathnames with special chars

I've a feeling CMD.exe (at least some versions) is ignoring the outer quotes, in other words this may have nothing to do with escaping the odd characters.

Try creating GO.cmd containing just
CALL "C:\^(ba^&tch^).bat"

Then
CMD /c go.cmd

Offline

#3 02 Dec 2005 10:07

ajw
Member
Registered: 01 Dec 2005
Posts: 2

Re: cmd.exe /c and pathnames with special chars

Thanks Simon.  Actually I'm launching cmd.exe programmatically.  Previously I did this by executing cmd.exe /c, and passing the pathname to the file as the program's arguments.  If the pathname contains a space I need to surround it with quotes.  Then I found that if the pathname contains special chars ( ) and & they have to be escaped with ^.  Theoretically there are other special chars that need escaping with ^, but they can't appear in file pathnames.  Then I found that if (and only if) the pathname contains escaped special chars, execution will still fail if the pathname contains a space anywhere (which normally is not a problem).  Ay caramba!  So now I've abandoned the cmd.exe /c approach, and just execute the pathname to the batch file, and hope the user hasn't done anything silly like disassociate .bat and .cmd files from the command interpreter.

andrew

Offline

Board footer

Powered by FluxBB