1

我在网上找到了这个批处理脚本,它采用文件名并根据文件名创建一个文件夹,然后将文件移动到该文件夹​​中:

for %%f in (*) do @(md "%%~nf"&move "%%f" "%%~nf")>nul 2>&1&1

我想获取相似的文件并将它们移动到同一个文件夹中,所有多个文件最后都有 001 002 003... 等,所以我只想根据除了最后的数字之外的所有内容对它们进行排序和移动。我做了一些挖掘,我认为我需要在该脚本中使用 %%var:~start,end%% ,但不是 100% 确定如何配置它。

所以如果我有:

render001.png
render002.png
render003.png

txt001.png
txt002.png
txt003.png

我只想要两个名为“render”和“txt”的文件夹,并将文件移到那里

任何帮助都是极好的。

4

1 回答 1

1

这是我的建议:

@Echo Off
:: List all files and call :move_2_subfolder -function one file at a time. 
FOR %%a IN (*.png) DO Call :move_2_subfolder "%%~a"
GoTo :End

:move_2_subfolder
:: Save the name of the file [without extension] to sub_folder_name -variable.
Set sub_folder_name=%~n1
:: Strip the numbers off. 
Set sub_folder_name=%sub_folder_name:0=%
Set sub_folder_name=%sub_folder_name:1=%
Set sub_folder_name=%sub_folder_name:2=%
Set sub_folder_name=%sub_folder_name:3=%
Set sub_folder_name=%sub_folder_name:4=%
Set sub_folder_name=%sub_folder_name:5=%
Set sub_folder_name=%sub_folder_name:6=%
Set sub_folder_name=%sub_folder_name:7=%
Set sub_folder_name=%sub_folder_name:8=%
Set sub_folder_name=%sub_folder_name:9=%
:: Create folder, if necessary.
If not exist "%sub_folder_name%" md "%sub_folder_name%\" 
:: Move the file.
Move "%~1" "%sub_folder_name%\"
:: Return to For -command. 
GoTo :EOF

:End
If Defined sub_folder_name Set sub_folder_name=
@Echo Off

这不起作用:

For /L %%a IN (0,1,9) DO Set sub_folder_name=%sub_folder_name:%%~a=%*

这也不会:

...
For /L %%a IN (0,1,9) DO Call :strip_numbers "%%~a"

:strip_numbers
Set sub_folder_name=%sub_folder_name:%~1%
GoTo :EOF

出于某种原因,Set -command 不接受%%~a%~1变量。

于 2012-10-17T11:27:45.617 回答