1

我正在尝试使用通配符将某些文件组移动到由该组的文件名创建的文件夹中。文件名存储在“events.txt”中。我的批处理文件一直运行到最后一行。显示我的语法不正确。

echo off
for /F "tokens=*" %%A in (events.txt) do call :makemove %%A
pause 
exit

:makemove
set f=%1
set file=%f:~0,-4%  
md X%file%
set dest=C:\Users\sony\Desktop\X%file%
move /y "C:\Users\sony\Desktop\*%file%*.*" "%dest%"  
4

2 回答 2

2

好像后面有空格字符

set file=%f:~0,-4%

线。

这会导致脚本的最后一行评估为

move /y "C:\Users\sony\Desktop\*foobar  *.*" "C:\Users\sony\Desktop\Xfoobar  "

并弄乱了路径。

于 2012-06-15T12:32:08.103 回答
2

Like Helbreder pointed out, there is a space after set file=%f:~0,-4%.
To avoid this type of problems you can use the extended syntax of SET.

set "file=%f:~0,-4%"

The surrounding quotes will ensure that only all characters until the last quote are part of the string.
The quotes itself are not part of the string.

So even this would work

set "file=%f:~0,-4%"    the spaces and this text will be removed

Another positive effect from the quotes is that they will avoid problems with special characters in the filename, like in Cat&Dog.

So your code should look like

@echo off
for /F "tokens=*" %%A in (events.txt) do call :makemove %%A
pause 
exit

:makemove
set "f=%~1"
set "file=%f:~0,-4%"
md "X%file%"
set "dest=C:\Users\sony\Desktop\X%file%"
move /y "C:\Users\sony\Desktop\*%file%*.*" "%dest%"
于 2012-06-18T04:33:45.650 回答