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

Posted on 2018-01-12
Medium Priority
Last Modified: 2018-01-16
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

Question by:Mark Lewis
LVL 12

Expert Comment

ID: 42433282
Do the filenames have an exact number of spaces before the part that differs (like after the second space in your example filenames)?
LVL 61

Expert Comment

by:Bill Prew
ID: 42433292
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?


Expert Comment

by:Jan Louwerens
ID: 42433293
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.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

LVL 61

Accepted Solution

Bill Prew earned 2000 total points
ID: 42433319
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

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

Open in new window


Author Comment

by:Mark Lewis
ID: 42433364
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
LVL 61

Expert Comment

by:Bill Prew
ID: 42433772
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.


Author Closing Comment

by:Mark Lewis
ID: 42436762
Worked like a charm. Thanks sir!
LVL 61

Expert Comment

by:Bill Prew
ID: 42436765
Great that was helpful, and thanks for the feedback.,


Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…
In the video, one can understand the process of resizing images in single or bulk. Kernel Bulk Image Resizer is an easy to use tool for resizing large number of images. One can add and resize multiple images with this tool in single go. The video sh…

568 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question