Help needed, Spliting filenames into folders

After a lot of searching I've found enough to tell me that Opus is capable of doing this, but haven't managed to piece enough together to actually do what I need.
I have a substantial number of files which are in the format

author - title.extension
or author - location volume - title.extension

What I'd like to do is reorganise these into folders, so that
\author\title.extension and
\author\location\volume title.extension

Volume is always a number with a value 0 to 99 (and it would be useful to add a leading 0 to any single digit volume number)

An additional wrinkle is that it is feasible that this will produce duplicates of title.extension and this would best be resolved by adding a number to the end of title (and further incrementing it if necessary)

(I inherited this mess, and am trying to get it back to a being usable resource)

Searching through the forums, I've found a lot of helpful posts that indicate any separate part of this is doable (and often several ways of doing it) but haven't managed to put a solution together for the whole problem myself.

Hope the above makes sense.

Could you give a few real examples of current paths and what you want them to turn into?

Thank you for the fast response Leo

The path will vary as these files are strewn across various backup media and old hardrives.
The current sample I'm trying to sort in order to test solutions are
C:\Documents and Settings\nuatha\My Documents\to sort
Peter Allenby - Resources.doc
Peter Allenby - ME Handout.pdf
J Trotter - MEBulletin 04 - Qi Chong.rtf
J Trotter - MEBulletin 04 - Meeting Report.rtf
J Trotter - MEBulletin 05 - CMDA.rtf
J Trotter - Spring Into.doc
P Goodall - seeing differently.txt
P Goodall - MAPS - A New Guide Dog.txt
Jefferson - Lions 7 - Annual Report.wps
Johnson - biblio.txt

Ideally (having added an initial letter directory to my original request)
C:\Documents and Settings\nuatha\My Documents\sorted
\P\Peter Allenby\Resources.doc
\P\Peter Allenby\ME Handout.pdf
\J\J Trotter\Spring Into.doc
\J\J Trotter\MEBulletin\04 Qi Chong.rtf
\J\J Trotter\MEBulletin\04 Meeting Report.rtf
\J\J Trotter\MEBulletin\05 CMDA.rtf
\J\J Trotter\Spring Into.doc
\P\P Goodall\seeing differently.txt
\P\P Goodall\MAPS\A New Guide Dog.txt
\J\Jefferson\Lions\07 Annual Report.wps
\J\Johnson\biblio.txt

I'm aware that the initial folder won't be ideal (indexing on first letter will often mean first name) but its an attempt to reduce the sheer number of sub folders I will end up with in a single directory.

Does the above help explain it?

I've found Leo's post
[Various simple rename presets)
Various simple rename presets
Which has presets to do almost all of what I need.

using 07 - Move to Artist Subdir.orp twice will sort author then location
I'd appreciate guidance on how to detect whether there are numbers in the middle section of the filename and if so to move the numbers from one side of the hyphen to the other.

Is it possible to string several presets together snd run them sequentially
Or am I going about this completely the wrong way?

That's a complex rename with branching/conditions so it's best done using a rename-script instead of a single regex.

I've made a script which should do what you want. It's based on the Script to perform multiple Regular Expressions but uses branching logic to make sure it only applies the most appropriate transformation to each filename.

The script is attached to the bottom of this message as a .orp file.

How to import an Opus rename preset .orp file

To load a preset into your Rename dialog, make sure the dialog is in Advanced mode, then select Import... from the File menu and select the .orp file attached to the bottom of this post. That loads the preset from disk but only temporarily. To add it to your personal list of presets, click the Add button above the list on the right and give it a name.

The script

If you want to use the script as-is, download and import the attached .orp file.

I'm just reproducing the script here so that people interested in rename-scripting can read it and use it for ideas without the hassle of downloading and importing it.

[code]@script vbscript
option explicit

Function Rename_GetNewName ( strFileName, strFullPath, fIsFolder, strOldName, ByRef strNewName )

Dim re
Dim strWordArray
Dim strExtension
Dim strNameOnly
Dim fGotMatch

fGotMatch = False

' Create a RegExp object. See http://msdn2.microsoft.com/en-us/library/ms974570.aspx
Set re = new RegExp
re.IgnoreCase = True ' Case-insensitive matching.
re.Global = False ' Don't replace all matches.

' If we're renaming a file then remove the extension from the end and save it for later.
if fIsFolder or 0 = InStr(strFileName,".") then
	strExtension = ""
	strNameOnly = strFileName
else
	strExtension = Right(strFileName, Len(strFileName)-(InStrRev(strFileName,".")-1))
	strNameOnly = Left(strFileName, InStrRev(strFileName,".")-1)
end if

' Apply our regular expressions to the filename.

' "J Trotter - MEBulletin 4 - Qi Chong" -->
' "J Trotter\MEBulletin\04 Qi Chong"
re.Pattern = "^([^-]+) - ([^-]+) ([0-9]) - (.+)$"
if (re.Test(strNameOnly)) then
	fGotMatch = True
	strNameOnly = re.Replace(strNameOnly, "$1\$2\0$3 $4")
else
	' "J Trotter - MEBulletin 04 - Qi Chong" -->
	' "J Trotter\MEBulletin\04 Qi Chong"
	re.Pattern = "^([^-]+) - ([^-]+) ([0-9]+) - (.+)$"
	if (re.Test(strNameOnly)) then
		fGotMatch = True
		strNameOnly = re.Replace(strNameOnly, "$1\$2\$3 $4")
	else
		' "J Trotter - Spring Into" -->
		' "J Trotter\Spring Into"
		re.Pattern = "^([^-]+) - (.+)$"
		if (re.Test(strNameOnly)) then
			fGotMatch = True
			strNameOnly = re.Replace(strNameOnly, "$1\$2")
		end if
	end if
end if

if fGotMatch then
	' If the path matched our expected format and was changed,
	' find any remaining " - " in the path and turn them into
	' subfolders.
	re.Global = True ' Replace all matches, not just the first " - "
	re.Pattern = "^(.+) - (.+)$"
	strNameOnly = re.Replace(strNameOnly, "$1\$2")
	re.Global = False

	' If the path matched our expected format and was changed,
	' and it starts with a letter or number, move it into
	' a different folder.
	re.Pattern = "^(.)(.+)$"
	strNameOnly = re.Replace(strNameOnly, "..\sorted\$1\$1$2")
end if

' Rejoin the name and extension and we're finished
strNewName = strNameOnly & strExtension

End Function[/code]
Multi-RegExp-nuatha.orp (2.79 KB)


Thank you very much for this, and particularly the commenting - there's a whole tutorial in there.

It dealt with my test folder perfectly.
I'll try it against a large batch of files soon and post results.
Thanks again

Thanks for the great vb examples!!
I'm trying to add:
' look for , and replace with space
re.Pattern = "(
)"
strNameOnly = re.Replace(strNameOnly, " ")

 ' Add -
 re.Pattern = "([0-9]+) (.*)"
 strNameOnly = re.Replace(strNameOnly, "$1 - $2")

How do I allow all matches on the first set of expressions and not the second?

Put re.Global = True before the first set and re.Global = False before the second. Search for re.Global in the script to see some examples that do that already.