2012-07-01 70 views
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

Drag and drop batch file for multiple files?

Batch Processing of Multiple Files in Multiple Folders

+3

的链接也被描述你能告诉我们你在哪里卡住了?这将是有益的 – jeb

+0

老实说,我只是不够聪明。我需要批处理文件来处理具有特殊字符和空格的文件。 – user1136386

回答

0

用拖动处理特殊字符&拖放很棘手,因为没有以可靠的方式引用它们。

空间不是很复杂,因为带空格的文件名会自动引用。
但是有两个特殊字符,可以产生问题,感叹号和&符号。

与符号的名称将不会自动报价,所以该批次可以这样调用

myBatch.bat Cat&Dog.txt 

这就产生了两个问题,第一个参数是不完整的。
%1%*中只有文字Cat&Dog.txt部分不能通过正常参数访问,而是通过cmdcmdline变量访问。
这应该通过延迟扩展进行扩展,否则可以从文件名中删除感叹号和插入符号。
并且当该批处理结束时,它应该使用exit命令关闭cmd窗口,否则&Dog.txt将执行并且通常会产生错误。

所以读完filenamelist应该像

@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 

这是在你refereneced Drag and drop batch file for multiple files?