Need help with complicated renaming please

A swapping-ground for Regular Expression syntax

Need help with complicated renaming please

Postby speedrider » Tue Jan 30, 2018 11:15 am

Hi,
hope I can get some help here with my renaming procedure. I simply don't get it... :roll:

I have filenames in this scheme:

Countrycode-FirstnameLastname-FirstnameLastname or Countrycode-FirstnameLastname-FirstnameLastname-FirstnameLastname

Example:
GER-MartinSchmidt-NinaWagner
or
UK-PeterFord-JeremyJordan-LisaWendt

I need to mass rename these to:
GER - Martin Schmidt and Nina Wagner
UK - Peter Ford, Jeremy Jordan and Lisa Wendt

So what I need is:
1) Get spaces before all capitals except for the first part (country) -> Add Word Space makes this for all occurencies in the name (?) but I need this only after the first separator "-"
2) rename the last "-" to "and"
3) in case of three or more names in the filename rename the other "-" to a comma ","

I know it's complicated and I am not sure if I can do this with this tool.
Hopefully someone can help as I need to do this with thousands of files.

Thanks so much in advance!
speedrider
 
Posts: 4
Joined: Tue Jan 30, 2018 11:02 am

Re: Need help with complicated renaming please

Postby dude » Tue Sep 25, 2018 10:14 am

Definitively possible.

2 questions before I spend any time on this:
- Is this still an issue?
- Can't see that you put any effort in it yourself (beside asking on a forum). What did you do thus far?
dude
 

Re: Need help with complicated renaming please

Postby speedrider » Tue Sep 25, 2018 10:27 am

Hey dude :D

Thanks for your reply.

Yes, unfortunately it is still an issue. Right now we do all this manually when we find some time...

I read a lot about regex and renaming procedures on this forum (and others) but I'm sorry I am not very familiar with it and the results were disappointing so far.

If you can help out we all would be very happy. I have hope when you say it's definitely posssible and you have the right skills. :-)

Thanks a lot in advance!
speedrider
 
Posts: 4
Joined: Tue Jan 30, 2018 11:02 am

Re: Need help with complicated renaming please

Postby dude » Tue Sep 25, 2018 10:38 am

- Are all these files in 1 folder?
- Are the renamed files in the same folder(s) as the "todo" files?
- Are there any other files in those folders?
- what is/are the file extension(s) (like: .txt or .doc)
- How many files are already renamed (estimated number or percentage)
Last edited by dude on Tue Sep 25, 2018 11:09 am, edited 1 time in total.
dude
 

Re: Need help with complicated renaming please

Postby dude » Tue Sep 25, 2018 11:07 am

Ok, got it working. Waiting for your input.
dude
 

Re: Need help with complicated renaming please

Postby speedrider » Tue Sep 25, 2018 11:34 am

dude wrote:- Are all these files in 1 folder?

Yes, they are.

- Are the renamed files in the same folder(s) as the "todo" files?

No.

- Are there any other files in those folders?

No, just the files that need to be renamed.

- what is/are the file extension(s) (like: .txt or .doc)

.docx

- How many files are already renamed (estimated number or percentage)

Do you mean manually renamed? These are already moved to different folders. So in that folder "stilltoberenamed" there are only files that still need renaming.



Your help will save us so much work! Thanks again. :D
speedrider
 
Posts: 4
Joined: Tue Jan 30, 2018 11:02 am

Re: Need help with complicated renaming please

Postby dude » Tue Sep 25, 2018 1:48 pm

Well then ..

First test
  1. Create an empty folder, let's say c:\test
  2. copy a couple of your files to this folder (so we don't mess up the original ones)
  3. In File Explorer, browse to c:\test
  4. In the address bar, type powershell (and press Enter)
  5. Check if you are indeed in the C:\temp folder (see prompt)
  6. Issue command: mode 300,60 (+ Enter) to get a "wide screen"
  7. issue command (+Enter):
    Code: Select all
    gi *-*.docx -Exclude "* - *"| % {rename-item  -whatif -path $_.FullName -NewName ($_.Name.Tostring() -cReplace( '([A-Z][a-z]+)'," `$1") -creplace('(.*)- (.*)', '$1 and $2') -replace('-',',') -creplace('^([A-Z]{2,}),','$1 -'))}

    Yes, that is one line :)
    This will do a rename simulation.
  8. Check the output
  9. If output is as expected, remove the -whatif from the command and run it again
  10. Check the output
  11. Report back



This would also be possible in Bulk Rename Utility, but I have never used it (and probably never will).

NB: This could be optimized further (less steps/iterations) but you probably don't care about that (and frankly: neither do I :D)
dude
 

Re: Need help with complicated renaming please

Postby speedrider » Tue Sep 25, 2018 3:06 pm

^^ This works absolutely great and is exactly what we needed. Never thought of powershell.

I am so thankful, dude! You are my renaming master now. If I was a girl, I'd kiss you all over now. :D
speedrider
 
Posts: 4
Joined: Tue Jan 30, 2018 11:02 am

Re: Need help with complicated renaming please

Postby dude » Tue Sep 25, 2018 6:44 pm

Hahahaha! :D

You're welcome! Enjoy the time you gained with this script!
dude
 

Re: Need help with complicated renaming please

Postby trm2 » Thu Jan 16, 2020 10:44 pm

In case you are wondering how to do it in BRU, here it is:

Match: (.+?)-([A-Z])(.+?)([A-Z])(.+?)-([A-Z])(.+?)([A-Z])(.*)
Replace: \1 - \2\3 \4\5 and \6\7 \8\9

Where:

\1 = GER
\2 = M
\3 = artin
\4 = S
\5 = chmidt
\6 = N
\7 = ina
\8 = W
\9 = agner
trm2
 
Posts: 156
Joined: Wed Jan 15, 2020 12:47 pm

Re: Need help with complicated renaming please

Postby trm2 » Sun Jan 19, 2020 8:54 am

The first one I posted handled two names. This one will handle three names.
This one is more universal and will work with any Country’s abbreviation – i.e., USA

Match: (.+?)-([A-Z].*)([A-Z].*)-(.*)([A-Z].*)-([A-Z].*).*?([A-Z].*)
Replace: \1-\2 \3 and \4 \5 and \6 \7

Where:

\1 = UK
\2 = Peter
\3 = Ford
\4 = Jeremy
\5 = Jordan
\6 = Lisa
\7 = Wendt
trm2
 
Posts: 156
Joined: Wed Jan 15, 2020 12:47 pm

Re: Need help with complicated renaming please

Postby trm2 » Sun Jan 19, 2020 9:04 am

Finally, here is the RegEx to handle 4! names. That's all folks. There are no more Capture Groups to handle 5 names:

USA-PeterFord-JeremyJordan-LisaWendt-GeorgeJones

Match: (.+?)-([A-Z].*)([A-Z].*)-(.*)([A-Z].*)-([A-Z].*).*?([A-Z].*).*?-([A-Z].*).*?([A-Z].*)
Replace: \1-\2 \3 and \4 \5 and \6 \7 and \8 \9

Where:

\1 = UK
\2 = Peter
\3 = Ford
\4 = Jeremy
\5 = Jordan
\6 = Lisa
\7 = Wendt
\8 = George
\9 = Jones
trm2
 
Posts: 156
Joined: Wed Jan 15, 2020 12:47 pm

Re: Need help with complicated renaming please

Postby bru » Sun Feb 02, 2020 4:36 pm

And since I like a challenge, here's a batch using brc32 to handle anywhere from 1-10 names
Remove /Execute to preview renames, & edit line2 with a folder-path:

@echo off
cd "C:\YourFolderPath"
set reg1=/Regexp:"^([A-Z]{2,})-([A-Z][^A-Z -]*?)([A-Z].*)$:\1|\2 \3"
set reg2=/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
set max10=%reg2% %reg2% %reg2% %reg2% %reg2% %reg2% %reg2% %reg2% %reg2%
set reg3=/Regexp:"(.*[|].*), (.*):\1 and \2"
set reg4=/Regexp:"(.*)[|](.*):\1 - \2"
brc32 /Pattern:*-*.docx %reg1% %max10% %reg3% %reg4% /Execute
pause>nul

It wont touch already-renamed files (or any file with spaces), no need to move them after renaming:
Code: Select all
Sample Filenames                                                  Results
CAN-FirstLast.docx ----------------------------------------> CAN - First Last.docx
CAN-First Last.docx ---------------------------------------> (No effect)
CAN - FirstLast.docx --------------------------------------> (No effect)
UK-JoLii1-JoLii2-JoLii3-JoLii4-JoLii5-JoLii6 --> UK - Jo Lii1, Jo Lii2, Jo Lii3, Jo Lii4, Jo Lii5 and Jo Lii6
bru
 
Posts: 62
Joined: Wed Jan 31, 2018 7:35 pm

Re: Need help with complicated renaming please

Postby trm2 » Wed Feb 05, 2020 7:07 pm

I like your idea, since Brc can run multiple RegEx in a single run it can handle much more than the 3 or 4 names I was struggling with.

Unfamiliar as I am though with BRC at the moment, I have a problem running it:

It comes up with an error 3 when looking at the log - 'can't find specified path'. I have recreated your samples and placed them in a directory h:\A Test Folder.

It changes to the directory so that path is correct.
The files - all of the samples give reside in the single directory - but of course the program as far as I can tell never gets that far. Besides if it was a
matter of a missing file it would return an error code 2.

Samples:

CAN - FirstLast.docx
CAN-First Last.docx
CAN-FirstLast.docx
UK-JoLii1-JoLii2-JoLii3-JoLii4-JoLii5-JoLii6


Here is the code:

rem @echo off
cd "h:\A Test folder"
set reg1=/Regexp:"^([A-Z]{2,})-([A-Z][^A-Z -]*?)([A-Z].*)$:\1|\2 \3"
set reg2=/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
set max10=%reg2% %reg2% %reg2% %reg2% %reg2% %reg2% %reg2% %reg2% %reg2%
set reg3=/Regexp:"(.*[|].*), (.*):\1 and \2"
set reg4=/Regexp:"(.*)[|](.*):\1 - \2"
H:\BRC_Unicode_64\brc64.exe /Pattern:*-*.docx %reg1% %max10% %reg3% %reg4%
pause>nul
EXIT /B %ERRORLEVEL%


I need the preview results so I left off /EXECUTE and added the return code

My understanding of the batch is as follows:

1. set Assign a Regex to each environmental variable identified as: reg1, reg2, max10, reg3 and reg4
2. brc32 Executes the Brc program
3. /Pattern: *.*.docx Process all Word docx files found in directory
4. % Execute all RegEx in order specified

When run:

Processing Folder h:\A Test Folder\
This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
h:\A Test Folder>pausel>nul

Log:

--------------------------------------------------------------------------------------------------------
H:\BRC_Unicode_64>rem @echo off

H:\BRC_Unicode_64>cd "h:\A Test folder"

h:\A Test Folder>set reg1=/Regexp:"^([A-Z]{2,})-([A-Z][^A-Z -]*?)([A-Z].*)$:\1|\2 \3"

h:\A Test Folder>set reg2=/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"

h:\A Test Folder>set max10=
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"

h:\A Test Folder>set reg3=/Regexp:"(.*[|].*), (.*):\1 and \2"

h:\A Test Folder>set reg4=/Regexp:"(.*)[|](.*):\1 - \2"

h:\A Test Folder>H:\BRC_Unicode_64\brc64.exe /Pattern:*-*.docx
/Regexp:"^([A-Z]{2,})-([A-Z][^A-Z -]*?)([A-Z].*)$:\1|\2 \3"

/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"^(.*[|][^-]*)-([A-Z][^A-Z -]*?)([A-Z].*)$:\1, \2 \3"
/Regexp:"(.*[|].*), (.*):\1 and \2" /Regexp:"(.*)[|](.*):\1 - \2"

h:\A Test Folder>pause1>nul

h:\A Test Folder>EXIT /B 3
-------------------------------------------------------------------------------------------

Further questions -

1. Your last sample, beginning with UK with no extension, perhaps this is overlooked - should be docx, otherwise it won't get processed because your
pattern specifies *.docx files only?
2. What happens without the /EXECUTE parameter - I mean how does it display the rename preview unless it brings up the GUI version momentarily?

bru, please assist.

Thanks in advance.
trm2
 
Posts: 156
Joined: Wed Jan 15, 2020 12:47 pm

Re: Need help with complicated renaming please

Postby trm2 » Wed Feb 05, 2020 8:12 pm

Okay, so running the batch file as admin removed error message, but files not renamed.

Also tried 32 bit version of program to make sure that wasn't the problem. It wasn't.

Still need assistance, but this time with your coding. I can't get it work.

Thanks.
trm2
 
Posts: 156
Joined: Wed Jan 15, 2020 12:47 pm

Next

Return to Regular Expressions


cron