【问题标题】:Batch Drag and drop files and folders批量拖放文件和文件夹
【发布时间】:2012-07-01 19:12:04
【问题描述】:

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

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

【问题讨论】:

  • 你能告诉我们你卡在哪里了吗?会有帮助的
  • 老实说,我只是不够聪明。我需要批处理来处理带有特殊字符和空格的文件。

标签: windows-7 batch-file


【解决方案1】:

使用 Drag&Drop 处理特殊字符很棘手,因为不能以可靠的方式引用它们。

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

带有和号的名称不会自动引用,因此可以这样调用批处理

myBatch.bat Cat&Dog.txt

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

所以读取文件名列表应该是这样的

@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

这在您引用的链接Drag and drop batch file for multiple files?中也有描述

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    相关资源
    最近更新 更多