Batch move files by filename into folders

September 30, 2011 at 07:14:12
Specs: Windows XP, Core2Duo 2.8ghz/4gb
I'm trying to batch move files by filename into folders based on the first 7 characters of the filename.

example of the files I have:
C:\files\ec-1001-0.jpg
C:\files\ec-1001-1.jpg
C:\files\ec-1001-2.jpg
C:\files\ec-1001-3.jpg
C:\files\ec-1001-4.jpg
C:\files\ec-1002-0.jpg
C:\files\ec-1002-1.jpg
C:\files\ec-1003-1.jpg
etc up to ...\ec-4000-4.jpg

They are all currently in one folder (obviously), and they go from 1001-4000, and it will be an ongoing thing as 4000 will become 400,000 over the next few months.

I would like them to take the first 7 characters of the filename (in the ex: ec-1001) and move them into folders of that name.
ex:
c:\files\ec-1001\ec-1001-0.jpg c:\files\ec-1001\ec-1001-1.jpg ..... etc etc etc....

It can create the folder if it doesn't exist, or I can create the folders if need be.

Any suggestions??? Will save me hours!


See More: Batch move files by filename into folders

Report •


#1
September 30, 2011 at 17:15:07
Please test this, no allowance is made for paths/filenames containing spaces. Change Copy to Move when you are satisfied with the output results:

@echo off
setlocal enabledelayedexpansion
cls

pushd c:\files\

for /f "tokens=*" %%1 in ('dir /a-d /b c:\files\*.jpg') do (
    set filename=%%1&set dirname=!filename:~0,7!
        
    if not exist c:\files\!dirname! (md c:\files\!dirname!)
    copy %%1 c:\files\!dirname!\>nul
)


Please come back & tell us if your problem is resolved.


Report •

#2
December 31, 2011 at 11:42:16
Hello, just to let people know. This DOES work.

Is it possible to add one function. Imagine the folders already already exist. how do you get it to check the existing folder and only move files with the first 7 chars to folder with the same first 7 chars.

Thank you......


Report •

#3
December 31, 2011 at 13:37:44
I suspect that you may be experiencing a problem if the filename number exceeds 4 characters so that by just selecting the first 7 characters higher numbered directories are not being created?

e.g. ec-4000 and ec-40000000 will be moved to the same folder.

You did ask for just the first 7 characters to be used, do you now want the entire number to form the directory name?

Happy New Year.


Please come back & tell us if your problem is resolved.


Report •

Related Solutions

#4
January 1, 2012 at 19:25:44
Hello Wahine.

The folders are already created so the first 7 chars of the files will match a folder. So it is moving the files but it should not create any folder.

Thank you


Report •

#5
January 1, 2012 at 22:55:55
If the folders exist remove the If command line from the script and no checking will be done so no folders will be created.


Please come back & tell us if your problem is resolved.


Report •

#6
January 2, 2012 at 03:59:04
Hello Wahine,

I have tried what you have suggested, the code is located below. I get a flash of the DOS prompt and then nothing. It seems not to work.
Any advice is more than welcomed.

Thank you

@echo off
setlocal enabledelayedexpansion
cls
pushd c:\folders\
for /f "tokens=*" %%1 in ('dir /a-d /b c:\folders\*') do (set filename=%%1&set dirname=! filename:~0,7!)


Report •

#7
January 2, 2012 at 07:39:50
Hello, I have also tried this, no joy.

@echo off
setlocal enabledelayedexpansion
cls
pushd c:\folders\
for /f "tokens=*" %%1 in ('dir /a-d /b
c:\folders\*') do (
set filename=%%1&set dirname=!
filename:~0,7!
copy %%1 c:\folders\!dirname!\>nul
)


Report •

#8
January 2, 2012 at 12:13:40
There could be many causes of the failure including:

1. There are no files to copy.
2. The output folder name does not exist.

Only you can check for these possibilities.

Please run this from the Command Prompt and advise of any error message(s) displayed.

@echo on
setlocal enabledelayedexpansion
cls

pushd c:\files\

for /f "tokens=*" %%1 in ('dir /a-d /b c:\files\*.jpg') do (
    set filename=%%1&set dirname=!filename:~0,7!
        
    REM if not exist c:\files\!dirname! (md c:\files\!dirname!)
    copy %%1 c:\files\!dirname!\>nul
)


Please come back & tell us if your problem is resolved.


Report •

#9
January 2, 2012 at 13:23:30
Hello Wahine,

This is the message I get. Please note that the folders are LONGER than 7 chars however the the first 7 char are the identifier.

Also i forgot to say that it is not exclusive to ONLY jpg, I would like ALL files.

Thank you ever so much.

C:\folders>pushd c:\folders\

C:\folders>for /F "tokens=*" %1 in ('dir /a-d /b c:\folders\*.jpg') do (set file
name=%1  & set dirname=!filename:~0,7!
 REM if not exist c:\folders\!dirname! (md c:\folders\!dirname!)

copy %1 c:\folders\!dirname!\ 1>nul )

C:\folders>(set filename=004-10-business-terminologies-job-titles.jpg  & set dir
name=!filename:~0,7!
 REM if not exist c:\folders\!dirname! (md c:\folders\!dirname!)
 copy 004-10-business-terminologies-job-titles.jpg c:\folders\!dirname!\ 1>nul )

The system cannot find the path specified.

C:\folders>pause
Press any key to continue . . .


Report •

#10
January 2, 2012 at 13:26:41
Just as a follow up. If I rename:

004-10-business-terminologies-job-titles

to

004-10-
it works.

Which means I need it to look for ONLY the first 7 chars but ignore the rest of the name.

Thank you


Report •

#11
January 2, 2012 at 18:32:24
Sorry, I'm almost totally confused, might be a reaction to the New Year celebrations.

You say "Please note that the folders are LONGER than 7 chars however the the first 7 char are the identifier ". Are you saying that the already created folder name is 004-10-business-terminologies-job-titles and to that folder you wish to copy 004-10-business-terminologies-job-titles.jpg but you want to only interrogate the first 7 characters of the folder name to match the first 7 characters of the filename so that all files with 004-10- as the first seven characters will be copied to the same folder? This is reasonably easy to achieve but let's settle on exactly what you want first.

Thanks


Please come back & tell us if your problem is resolved.


Report •

#12
January 3, 2012 at 02:47:09
Hello Wahine.

Yes that is exactly it.

- Folders are already made
- First 7 chars are the identifier
- I wish to MOVE them, not copy.

Sorry for not explain well. I am new to this so I will have to practice a lot.

Thank you


Report •

#13
January 3, 2012 at 14:11:18
Now I think we are on same track. The following script may be what's required. It must as usual be considered as untested. If you are confident that the output is correct change Copy to Move. To process all files change *.jpg to *.*

Below is the file structure prior to running the script:

 Directory of C:\files

Wed 04/01/2012  10:26    <DIR>          .
Wed 04/01/2012  10:26    <DIR>          ..
Wed 04/01/2012  10:26    <DIR>          ec-1001-Business-terminologies-job-titles
Wed 04/01/2012  10:26    <DIR>          ec-1002-Business-terminologies-job-titles
Wed 04/01/2012  10:26    <DIR>          ec-1003-Business-terminologies-job-titles

Wed 04/01/2012  10:21                 9 ec-1001-0.jpg
Wed 04/01/2012  10:21                 5 ec-1001-1.jpg
Wed 04/01/2012  10:22                 4 ec-1001-2.jpg
Wed 04/01/2012  10:22                 4 ec-1001-3.jpg
Wed 04/01/2012  10:23                 4 ec-1001-4.jpg
Wed 04/01/2012  10:23                 5 ec-1002-0.jpg
Wed 04/01/2012  10:24                12 ec-1002-1.jpg
Wed 04/01/2012  10:25                18 ec-1003-1.jpg

Script:

@echo off
setlocal enabledelayedexpansion
cls

pushd c:\files\

for /f "tokens=*" %%1 in ('dir /a-d /b *.jpg') do (
    set filename=%%1&set dirname=!filename:~0,7!

        for /f "tokens=*" %%A in ('dir /ad /b') do (
        set dirid=%%A&set dirid=!dirid:~0,7!

        if "!dirid!" equ "!dirname!" copy %%1 %%A
  )        
)

The file structure after Copying appears the same as before copying.

Sub-Directory contents are:


Directory of C:\files\ec-1001-Business-terminologies-job-titles

Wed 04/01/2012  10:40    <DIR>          .
Wed 04/01/2012  10:40    <DIR>          ..
Wed 04/01/2012  10:21                 9 ec-1001-0.jpg
Wed 04/01/2012  10:21                 5 ec-1001-1.jpg
Wed 04/01/2012  10:22                 4 ec-1001-2.jpg
Wed 04/01/2012  10:22                 4 ec-1001-3.jpg
Wed 04/01/2012  10:23                 4 ec-1001-4.jpg


Directory of C:\files\ec-1002-Business-terminologies-job-titles

Wed 04/01/2012  10:40    <DIR>          .
Wed 04/01/2012  10:40    <DIR>          ..
Wed 04/01/2012  10:23                 5 ec-1002-0.jpg
Wed 04/01/2012  10:24                12 ec-1002-1.jpg


Directory of C:\files\ec-1003-Business-terminologies-job-titles

Wed 04/01/2012  10:40    <DIR>          .
Wed 04/01/2012  10:40    <DIR>          ..
Wed 04/01/2012  10:25                18 ec-1003-1.jpg

I hope we can put this topic to bed now?? Good luck.


Please come back & tell us if your problem is resolved.


Report •

#14
January 3, 2012 at 16:46:39
It work a treat!!!!!!!!!!!!!!!!!!!

Thank you ever so much.

I changed the "copy" to a "move"

and also remove the JPG to do it on ALL FILES!

@echo on
setlocal enabledelayedexpansion
cls

pushd c:\folders\

for /f "tokens=*" %%1 in ('dir /a-d /b *') do (
    set filename=%%1&set dirname=!filename:~0,4!

        for /f "tokens=*" %%A in ('dir /ad /b') do (
        set dirid=%%A&set dirid=!dirid:~0,4!

        if "!dirid!" equ "!dirname!" move %%1 %%A
  )        
)

Thank you Wahine... You are great!!!!!!!!!!!!!!!!!!!!!


Report •

#15
January 3, 2012 at 17:06:00
Whew, at last! Great news. Thank you for coming back promptly to report your success.

Point to note is that if for any reason the matching directory does not exist, the corresponding file(s) will not be moved. So if you find that files still exist in the Files\ directory after running the script that is probably the reason.

Good luck.


Please come back & tell us if your problem is resolved.


Report •

#16
January 4, 2012 at 02:45:23
Thank you again Wahine,

I do have a side note, are there any videos or tutorials regarding batch processing and command line coding. I will be starting my epic journey with all this good stuff but of course I need to start somewhere.

Thank you


Report •

#17
January 4, 2012 at 19:24:15
There are many good and free batch scripting tutorials on the WWW, my favourite tutorial is by Rob Van Der Woude and can be found here http://www.robvanderwoude.com/

Batch scripts by Timo Salmi are also a good source of more advanced techniques but are possibly more advanced than you would want right now.


Please come back & tell us if your problem is resolved.


Report •


Ask Question