批量创建基于一系列文件的文件夹并移动到子目录

时间:2012-10-11 20:11:17

标签: batch-file dos

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

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'的文件夹,文件移到那里

任何帮助都会很棒。

1 个答案:

答案 0 :(得分: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 变量。