Batch create folders based on series of files and move to subdirectory

血红的双手。 提交于 2019-12-24 08:19:18

问题


I have this batch script I found online that takes a file name and makes a folder based on the name of the file and then moves the file into that folder:

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

I want to take similar files and move them into same folders, all multiple files have 001 002 003... ect at the end, so I just want to sort and move them based on everything besides the numbers on the end. I've did some digging and I think I need to use %%var:~start,end%% in that script, but not 100% sure how to configure it.

so if I have:

render001.png
render002.png
render003.png

and

txt001.png
txt002.png
txt003.png

I just want 2 folders named 'render' and 'txt' with the files moved into there

any help would be awesome.


回答1:


Here's my suggestion:

@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

This won't work:

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

Neither will this:

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

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

For some reason the Set -command doesn't accept the %%~a or %~1 variable.



来源:https://stackoverflow.com/questions/12847701/batch-create-folders-based-on-series-of-files-and-move-to-subdirectory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!