问题
I have thousands of JPGs named like this "aaa0001.jpg, aaa0002.jpg, aaa0003.jpg, bbb0001.jpg, bbb0002.jpg, bbb0003.jpg, ccc0001.jpg, ccc0002.jpg, ccc0003.jpg etc." in one folder.
I have created 26 folders like this aaa, bbb, ccc, ddd etc.
Is it possible to create a script that sets all the images in the appropriate folder?
Result "aaa0001.jpg, aaa0002.jpg, aaa0003.jpg" into folder "aaa", "bbb0001.jpg, bbb0002.jpg, bbb0003.jpg" into folder "bbb" etc.
Thank you!
My system is windows XP prof SP3...
回答1:
It would go like this in a Windows/dos batch file.
The statement %fp:~0,3%
determines which part of the filename is used as a foldername. 0,3 means: from the first character and the next 3 chars.
so a file named aaa001-01.jpg
will give a folder of aaa
.
To have files named abc001_03.jpg
go into folder 001
you change the statement to %fp:~3,3%
for %%a in (*.jpg) do call :copyfile %%a
goto :eof
:copyfile
set fp=%1
set folder=%fp:~0,3%
rem remove echo on the next line...
echo copy "%1" "%folder%"
rem or for moving: move /Y "%1" "%folder%"
goto :eof
回答2:
Just define the base path to create the news directorys in the VAR $path
@echo off
setlocal EnableDelayedExpansion
:::The path where the new Directorys will bw created
set $path="c:\Image\"
for %%a in (*.jpg) do (set $file="%%a"
set $Dir="%$path%CSV!$file:~4,3!"
if not exist "!$dir!" md "!$dir!"
move "!$file!" "!$dir!")
echo Terminated
来源:https://stackoverflow.com/questions/20328665/put-files-automatically-in-folders