Need help with a batch file to create folders based off filenames and move files to the folder

So here's the data I am looking at..I have a single folder files named this way:
file one (1).ext
file one (2).ext
file one (3).ext
file two 1.ext
file two 2.ext
file two 3.ext
file three a.ext
file three b.ext
file three c.ext ... etc etc

There are about 250,000 of these files in a single folder. I am trying to create a batch file where it will read the name of the file and create a folder based off of the text thats the same and stops when it reaches where the file is different and moves on to the next folder. Then copies the files into the created folder.

For example in the set above, I would end up with three folders: file one, file two and file the each with the 3 corresponding files moved to the correct folder. I came up with as much below but I cant figure out a way to get it to stop reading the file name when it reaches the "(" or the number or the different letter or whatever may be different in the file name. What I end up with is a folder for each file (full file name) and the corresponding file in that folder.

Heres my code:

@echo off
setlocal enabledelayedexpansion
for %%A in (*.ext) do (
   echo file found  %%A
   for /f "delims=" %%B in ("%%A") do set fname=%%~nB
   for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
   for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
   echo folder name !folname!
   if not exist "!folname!" (
      echo Folder !folname! does not exist, creating
      md "!folname!"
   ) else (
      echo Folder !folname! exists
   )
   echo Moving file %%A to folder !folname!
   move "%%A" "!folname!"
   )
echo Finished
pause

Thanks
Mark LewisAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aranaCommented:
Do the filenames have an exact number of spaces before the part that differs (like after the second space in your example filenames)?
0
Bill PrewCommented:
Need to know more about the file names.  

It looks like the components of the file name are separated by spaces?  Or are there other patterns?

Is it always the last component of each file name that needs to be ignored?

Can you provide a comprehensive assortment of the real world file names?


»bp
0
Jan LouwerensSoftware EngineerCommented:
In essence, what you want to do is to just take a substring of the of the existing filename. The tricky part is going to be in determining where to end the substring. Without some complex analysis of all the filenames within the folder, you're not really going to know what other filenames are like when deciding for each file where to end the substring. So then you have to decide where to end them ahead of time (This is the part that arana was hinting at.) If you know they all either have numbers or parenthesis when they start to differ, then you can find the index of the first number or open parenthesis, create the substring up to that index (removing any training spaces that may exist), then constructing a new filename from that.
0
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Bill PrewCommented:
If filename segments are separated by spaces, and if you wanted to strip off the last segment to get the folder name, here is a sample script that does that.  Seems to work the way you wanted with the sample file names you mentioned in the question.  Might give you some ideas.

@echo off
setlocal EnableDelayedExpansion

for %%F in (*.ext) do (
   call :Parse %%~nF
   if not exist "!folname!" (
      echo Folder Creating folder "!folname!"
      md "!folname!"
   )
   echo Moving "%%F" to "!folname!"
   move "%%F" "!folname!"
)
exit /b

:Parse
   set "folname=%~1"
:ParseLoop
   shift
   if "%~2" EQU "" exit /b
   set "folname=!folname! %~1"
   goto :ParseLoop

Open in new window


»bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark LewisAuthor Commented:
Ok, I went and took a cursory look at all quarter of a million files and 3/4 of them are a string of varying length but end with a control number enclosed in "()". That being said, I can easily use another script with a rename executable to make sure that all the control numbers and names are in between parenthesis. I have no problem with a 2 part batch ;). So, if I can assure that the file name just needs to be read to the "(" to create the folder name, does that make things easier? I still dont know why I cant figure this out but I do appreciate the help. A real world example of the files look like this"

Accounting audit company xyc part 328 329 330 2011_03_08 (78926854876965429863).pqw
Accounting audit company xxx part 1032 2009_11_22 (78926854876965429863).pqw
Accounting audit company xxx part 1032 2009_11_22 (78926855843385526557).pqw

You can see that xxx and xyc is the name of some company and can be varying length and I cant go on spacing because some files contain more than one part of the audit as shown in the first line. But essentially the last 2 would end up in a folder named the same as the file name. The control number in the ellipses would not be a part of the folder name obviously.

Thanks again for your help
0
Bill PrewCommented:
Okay, my script should work with those files, as it always removes the rightmost "chunk" of the filename, delimited by spaces.  Give it a test with some files there.


»bp
0
Mark LewisAuthor Commented:
Worked like a charm. Thanks sir!
0
Bill PrewCommented:
Great that was helpful, and thanks for the feedback.,


»bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.