Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

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.

@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

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.

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

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%

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |