0

我正在尝试使用我认为应该如下所示的解决方案从拖放选择中复制多个文件和文件夹:

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%
)

我尝试将以下示例中的代码放在一起,但我被卡住了:

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

拖放多个文件的批处理文件?

批量处理多个文件夹中的多个文件

4

1 回答 1

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?

于 2012-07-03T11:46:04.377 回答