Page 1 of 1

Automatically make directories and move files into them

Posted: 11 Apr 2010 13:13
by JHW
Everyone,

I have searched through this very helpful forum but haven't yet found the info I need.

I have a folder filled with 1,000-2,000 picture files. The file names are all in the format AAAA-BBB.ext where AAAA is a four-digit number (like 0602, 0513, 0845, etc.) BBB is a two- or three-digit number (12, 01, 003, etc.) and ext is tiff, jpg, psd, etc.

I am trying to write a batch script that will look at each file AAAA-BBB.ext and move it to a directory called AAAA (making that directory if it doesn't already exist), regardless of what BBB or ext are.

For example -- 0617-12.jpg would get moved to directory 0617/
0803-008.psd would get moved to directory 0803/
etc.

I am a newbie with batch scripts and have tried to begin at least by writing a batch script to make a series of directories, e.g. 0701, 0702, 0703, ... up to 0736. To do this, I wrote the following (I am using a console window in Windows Vista Home Premium):

Code: Select all

SETLOCAL ENABLEEXTENSIONS
for /L %%G in (0701,1,0736) DO mkdir %%G


When I execute this script, what it does instead is make a series of directories called 449, 450, 451, ... up to 478. Very strange! I suspect it has something to do with my leading zeroes.

Any ideas how to handle this? Or is there a quick script someone can point me to that will automate the entire task?

Thanks very much!

Re: Automatically make directories and move files into them

Posted: 11 Apr 2010 14:51
by !k
With "0" start octal numbers. For details see set /?
Use for /L %%G in (701,1,736) do mkdir 0%%G
Or better

Code: Select all

for /f "tokens=1* delims=-" %%i in ('dir /b *-*.jpg *-*.psd') do (
if not exist "%%i" md "%%i"
move "%%i-%%j" "%%i\%%i-%%j"
)

Re: Automatically make directories and move files into them

Posted: 11 Apr 2010 15:09
by JHW
Ah, I should have figured I was into octal territory.

The script you posted saved me hours of searching and frustration. I just added *-*.tif and *-*.tiff and it filed everything perfectly. I greatly appreciate your help! Spasibo!