Solved organise files based on string between two delimiters

November 8, 2012 at 06:40:43
Specs: Windows XP
Hi
I have a bunch of files I need to organise based on part of the filename.
The part of the filename is between 2 underscores e.g.
blah_56006-16_blah_(02)blah_ORGANISE_R_P11_24.ext

I want to create a folder called ORGANISE and place all files with ORGANISE between the 4th and 5th underscore into that folder

The string between the 4th and 5th underscore can vary in size, but should always
Be between the 4th and 5th underscore

Is this possible?


See More: organise files based on string between two delimiters

Report •


✔ Best Answer
November 9, 2012 at 07:44:11
Well, now I tested the script and found what went wrong: the missed /F switch in the FOR statement. This version is aimed to be executed inside the folder holding the files, just double clicking on it. If you want the new folders to be created inside the source one, remove the \ in front of %%e.

@echo off
for /F "tokens=1-5* delims=_" %%a in ('dir /B') do (
  if not exist "\%%e" md "\%%e"
  copy "%%a_%%b_%%c_%%d_%%e_%%f" "\%%e" >nul
)



#1
November 8, 2012 at 07:26:42
Well, if you just want to get all files with "_ORGANISE_" in the name, that's easy.
md ORGANISE
move *_ORGANISE_* ORGANISE

How To Ask Questions The Smart Way


Report •

#2
November 8, 2012 at 07:27:45
Here the script (untested). The required folder is created in the root of the current drive. If you want another location, add the wished path in front of "\%%e".
Replace copy with move if the files have to be deleted from the source location.

@echo off
pushd Your_Folder
for "tokens=1-5* delims=_" %%a in ('dir /B') do (
  if not exist "\%%e" md "\%%e"
  copy "%%a_%%b_%%c_%%d_%%e_%%f" "\%%e"
)
popd


Report •

#3
November 8, 2012 at 19:54:40
or, allowing for inconsistant naming format:
move *_*_*_*_ORGANISE_* organise

Report •

Related Solutions

#4
November 9, 2012 at 03:18:01
Sorry I guess I wasn't too clear. ORGANISE represents a variable string of characters which i need to organise by. It isn't always organise.
The whole filename is variable but I want to organise the files into folders based on what is between the 4th and 5th underscore

IVO's solution looks to be the best but I cant get it too work.

I've tried replacing the Your_Folder part with the path to my folder want sure if that was needed.

Ideally I'd like to just drop the batch script in the folder with the files and double click it. There will be various users using it on different sets of files


Report •

#5
November 9, 2012 at 06:40:39
If you launch the batch inside the folder holding the files, you can delete the pushd and popd statements. As I said I didn't test the script but carefully revised the code. Report your problem and I may try to help to put it at work.

Report •

#6
November 9, 2012 at 07:44:11
✔ Best Answer
Well, now I tested the script and found what went wrong: the missed /F switch in the FOR statement. This version is aimed to be executed inside the folder holding the files, just double clicking on it. If you want the new folders to be created inside the source one, remove the \ in front of %%e.

@echo off
for /F "tokens=1-5* delims=_" %%a in ('dir /B') do (
  if not exist "\%%e" md "\%%e"
  copy "%%a_%%b_%%c_%%d_%%e_%%f" "\%%e" >nul
)


Report •

#7
November 12, 2012 at 03:09:02
Works a treat thanks IVO!!

Just out of interest how would I have to alter the code to organise between different underscores, say the 3rd and 4th?


Report •

#8
November 12, 2012 at 06:10:24
Thank you, SpiderSprog, to have marked my script as Best Answer.

About your further request notice the script is aimed to organize selecting between 4th and 5th delimiter so you can see tokens=1-5* where a token is the string embraced by the selected delimiter. Each token then is named by a letter starting from the selected one (in my case %%a). To organize based on the 4th token the code has to be transformed

@echo off
for /F "tokens=1-4* delims=_" %%a in ('dir /B') do (
  if not exist "\%%d" md "\%%d"
  copy "%%a_%%b_%%c_%%d_%%e" "\%%d" >nul
)

Comparing the two versions you may be able to manipulate the code though ignoring the general syntax of the FOR /F statement (that is quite complex indeed).

Report •

#9
December 3, 2012 at 08:03:31
Thanks again IVO, nice to learn a little as I go along.
I had tried changing 1-5* to 1-4* but that didnt work. but I didn't realise I needed to change the line starting with copy also.

Thanks again for the code and pointers


Report •


Ask Question