• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 43
  • Last Modified:

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
0
Mark Lewis
Asked:
Mark Lewis
1 Solution
 
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 LouwerensCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
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
 
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

Join & Write a Comment

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now