Jouster500
Jouster500

Reputation: 782

Escape characters of a file path argument for a batch file

I was making a batch file to take dragged-and-dropped folders for program input. Everything was working fine until I passed a folder, which for the sake of this post, called foo&bar.

Checking what %1 contained inside the batch file looked like C:\path\to\foo or C:\path\to\foo\foo. If the file path were in quotes it would work, so the only working code that slightly takes this into effect is :

set arg1=%1
cd %arg1%*
set arg1="%CD%"

Which changes directory to the passed argument using wildcards. However this only works once for if there is another folder with un-escaped characters inside the parent folder, passing the child folder would result in the parent folders' value.

I tried the answer of this post, which suggests to output the argument using a remark and redirection statement during an @echo on sequence. However no progress occurred in rectifying the problem. Any suggestions?

To recap, I am looking for ways to pass folders with un-escaped characters as arguments to a batch file. The implementation should preferably be in a batch file, but answers using VBScript are welcome. However the starting program must be in batch as this is the only program of the 3 that accepts files as arguments.

To test this, create a batch file with following code:

@echo off
set "arg1=%~1"
echo "the passed path was %arg1%"
pause

Then create folders called foobar and foo&bar. Drag them onto the batch file to see their output. foo&bar will only return C:\path\to\foo.

Upvotes: 1

Views: 3813

Answers (1)

Harry Johnston
Harry Johnston

Reputation: 36328

OK, so the problem is that Explorer is passing this as the command line to cmd.exe:

C:\Windows\system32\cmd.exe /c ""C:\path\test.bat" C:\path\foo&bar"

The outermost quotes get stripped, and the command becomes

"C:\working\so46635563\test.bat" C:\path\foo&bar

which cmd.exe interprets similarly to

("C:\working\so46635563\test.bat" C:\path\foo) & bar

i.e., bar is considered to be a separate command, to be run after the batch file.


The best solution would be to drag-and-drop not directly onto the batch file but onto, say, a vbscript or a Powershell script or a plain old executable. That script could then run the batch file, either quoting the argument appropriately or putting the directory path into an environment variable rather than on the command line.


Alternatively, you can retrieve the original command string from %CMDCMDLINE% like this:

setlocal EnableDelayedExpansion
set "dirname=!CMDCMDLINE!"
set "dirname=%dirname:&=?%"
set "dirname=%dirname:" =*%"
set "dirname=%dirname:"=*%"
set "dirname=%dirname: =/%"
for /F "tokens=3 delims=*" %%i in ("%dirname%") do set dirname=%%i
set "dirname=%dirname:/= %"
set "dirname=%dirname:?=&%"
set dirname
pause
exit

Note the exit at the end; that is necessary so that cmd.exe doesn't try to run bar when it reaches the end of the script. Otherwise, if the part of the directory name after the & happens to be a valid command, it could cause trouble.

NB: I'm not sure how robust this script is.

I've tested it with the most obvious combinations, but YMMV. [It might be more sensible to use delayed expansion exclusively, I'm not sure. It doesn't seem to be necessary except in the first set command. Jeb's answer here might be a better choice if you're going this route.]


For the curious, the script works like this:

  • Load the original command line into dirname [necessary for the reason pointed out by jeb]
  • Replace all the & characters with ?
  • Replace all the quote marks with *
  • If a quote mark is followed by a space, suppress the space.

NB: it is necessary to suppress the space to deal with both the case where the path contains a space (in which case Explorer adds quote marks around it) and the case where it doesn't.

  • Replace all remaining spaces with /

NB: ? * and / are illegal in file names, so these replacements are safe.

At this point the string looks like this:

C:\Windows\system32\cmd.exe//c/**C:\path\test.bat**C:\path\foo?bar**

So we just need to pull out the third asterisk-delimited element, turn any forward slashes back into spaces and any question marks back into ampersands, and we're done. Phew!

Upvotes: 4

Related Questions