Handling of special characters with Drag&Drop is tricky, as doesn't quote them in a reliable way.
Spaces are not very complicated, as filenames with spaces are automatically quoted.
But there are two special characters, who can produce problems, the exclamation mark and the ampersand.
Names with an ampersand will not automatically quoted, so the batch can be called this way
myBatch.bat Cat&Dog.txt
This produces two problems, first the parameters aren't complete.
In %1
and also in %*
is only the text Cat
the &Dog.txt
part can't be accessed via the normal parameters, but via the cmdcmdline
variable.
This should be expanded via delayed expansion, else exclamation marks and carets can be removed from the filenames.
And when the batch ends, it should use the exit
command to close the cmd-window, else the &Dog.txt
will be executed and produce normally an error.
So reading the filenamelist should look like
@echo off
setlocal ENABLEDELAYEDEXPANSION
rem Take the cmd-line, remove all until the first parameter
set "params=!cmdcmdline:~0,-1!"
set "params=!params:*" =!"
set count=0
rem Split the parameters on spaces but respect the quotes
for %%N IN (!params!) do (
echo %%N
)
pause
REM ** The exit is important, so the cmd.ex doesn't try to execute commands after ampersands
exit
This is also described in the link you refereneced Drag and drop batch file for multiple files?