问题
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