Rename Script

Hi guys,

I need to rename a bunch of folders but I can't seem to do what I need in one rename.

These are the folders in question:

00QualityMatters
01Client
02StatutoryAuthorities
03ResidentEngineer
04Contractor
05Subcontractor
06Suppliers
07Certificates
08Valuations
09Subconsultants
10InternalCorrespondence
11Meetings
12Architects
13Surveyors
14Calculations
15Reports&Photographs
16Drawings
17DFS
19Drivethru
183DSMAX

I need to insert a space before all single capital letters and also to insert a space after the first two characters.

I use the following RegExp which renames most of the folders but fails on the folder "183DSMAX" where I need to enter a space before "3DSMAX".

Old Name: b([A-Z].*)#[/b]
New Name: \1 \2

Ideally I would like to add a space before and after the ampersand character as well although this isn't essential.

Any help would be much appreciated.

Cheers

Roly


So long as you don't have any folders that start with a lowercase letter after the first two digits, you can use this regexp to solve the "183DSMAX" problem:

(^[0-9][0-9]|.*[a-z]+)([0-9A-Z].*)#

Beyond that, though, it's stretching what you can do with a single regexp. Time for a script which uses multiple regexps instead.

The script below should do everything you want, including the & stuff. It's a slightly modified version of my Multi-RegExp script. Click "script mode" in the rename window and then paste the script in and click Refresh Preview just above the script box to see the results.

Note that VBScript's regexp syntax is a bit different to Opus's, so don't get confused by the $1 $2 vs \1 \2 difference.

The file extension stuff is redundant in this case but I left it in as it's a good thing to do in general.

[code]@script vbscript
option explicit

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

Dim re
Dim strWordArray
Dim strExtension
Dim strNameOnly

' Create a RegExp object. See http://msdn2.microsoft.com/en-us/library/ms974570.aspx
Set re = new RegExp
re.IgnoreCase = False ' Case-sensitive matching.
re.Global = True ' All matches will be replaced, not just the first match.

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

' Insert a space after the first two numbers, unless there's one already.
re.Pattern = "(^[0-9][0-9])([^ ])"
strNameOnly = re.Replace(strNameOnly, "$1 $2")

' Insert a space where a lowercase letter is before an uppercase letter.
re.Pattern = "([a-z])([A-Z])"
strNameOnly = re.Replace(strNameOnly, "$1 $2")

' Insert a space where missing before and after an ampersand.
re.Pattern = "([^ ])(&)"
strNameOnly = re.Replace(strNameOnly, "$1 $2")
re.Pattern = "(&)([^ ])"
strNameOnly = re.Replace(strNameOnly, "$1 $2")

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

End Function[/code]


This is superb as ever, just what I needed. Thanks again Leo.