Create folders based on file names?

Me
September 8, 2006 at 11:06:24
Specs: WinXP, 2Ghz

Does any one know of a batch script that can create folders based on file names and then move those files into the folders? For example I have two files 990355-0001 and 990355-0002. I need to make a folder 990355 and move both of these files into it. I don't really know anything about creating batch scripts.

See More: Create folders based on file names?

Report •


#1
September 9, 2006 at 00:23:54

blake, is this gonna be an on-going thing or just a one time deal? Unless it's on-goimg, why not simply create a new folder and name it the way you like and then drag/drop the desired files rather than bother with a script?
HTH.
Ed in Texas.

Report •

#2
September 9, 2006 at 00:41:28

@echo off
for /f %%F in ('dir/b/a-d 9*.') do call :sub1 %%F
goto :eof

:sub1
set name=%1
md %name:~0,6%
move %* %name:~0,6%

====================
Note that you'll get errors because on secomd and succeeding loops on a given prefix the directory will already exist.


=====================================
If at first you don't succeed, you're about average.

M2



Report •

#3
September 11, 2006 at 06:36:40

Ed in Texas, This is an on-going thing. I have several thousand files that I need to put in folders with similar file names.

Mechanix2Go, When I run the script I get a File Not Found error.


Report •

Related Solutions

#4
September 11, 2006 at 06:47:08

do this:

dir/b/a-d 9*.

and post a few lines of the result


=====================================
If at first you don't succeed, you're about average.

M2



Report •

#5
September 12, 2006 at 14:47:39

Ok I figured it out. It was because I used the wrong file names so I removed the 9 from the script. Thanks for the script.

Here are the changes I made:

@echo off
REM This script creates folders based on file names and moves those files into the folders.

REM *.jpg is the search term. Change this to search for different files.

REM md %name:~0,6% will make a directory based on the first 6 characters of the file name. Change to 5% for the first 5 characters.

REM move %* %name:~0,6% will move the file to the directory based on the first 6 characters of the file name. Change to 5% for the first 5 characters.

for /f %%F in ('dir/b/a-d *.jpg') do call :sub1 %%F
goto :eof

:sub1
set name=%1
md %name:~0,6%
move %* %name:~0,6%


Report •


Ask Question