0

I'm trying to copy multiple files and folders from a drag and drop selection using a solution I think should look something like this:

mkdir newdir
for %%a in ("%*") do (
echo %%a ^ >>new.set
)
for /f "tokens=* delims= " %%b in ('type "new.set"') do (
SET inset=%%b
call :folderchk
if "%diratr%"=="d" robocopy "%%b" "newdir" "*.*" "*.*" /B /E && exit /b
copy /Y %%b newdir
)

exit /b

:folderchk
for /f tokens=* delims= " %%c in ('dir /b %inset%') do (
set atr=%~ac
set diratr=%atr:~0,1%
)

I've tried throwing together code from the following examples but I'm stuck:

http://ss64.com/nt/syntax-dragdrop.html

Drag and drop batch file for multiple files?

Batch Processing of Multiple Files in Multiple Folders

Community
  • 1
  • 1

1 Answers1

1

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?

Community
  • 1
  • 1
jeb
  • 78,592
  • 17
  • 171
  • 225