A way to automate/batch folder renaming?

A swapping-ground for Regular Expression syntax

A way to automate/batch folder renaming?

Postby Dimsum » Sun May 28, 2006 9:17 pm

can BRU rename folders within a directory using certain conditions?

For example say I have folders named like:

xxxxxx-xxxxxx-xx-xx
xxx-xxxxxxxxx-xx-xx

And I want them to be renamed like:

xxxxxx - xxxxxx-xx-xx
xxx - xxxxxxxxx-xx-xx

Is BRU able to do this? Basically to read each folder and put a space on either side of the FIRST dash "-" within the folder name? It should also be known that if a folder already has a space on either side of the first "-" then it should ignore it.

thanks for any suggestions
Dimsum
 
Posts: 6
Joined: Sun May 28, 2006 9:09 pm
Location: London

Postby Admin » Sun May 28, 2006 10:08 pm

Yes, you could do that using a Regular Expression. I'd be tempted to do this in two stages though.

First off, I'd change all occurrences of " - " to "-". That way you know that none of the filenames already have the spaces.

Then I'd have a regular expression which finds a text string, followed by a dash, followed by a text string. Your RexExp replacement string will simply be the same again but with spaces either side.

So my first attempt, which looks like it does the job, would be:

Match: ([A-Za-z]*)(\-)(.*)
Replace: \1 \2 \3


Jim
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Postby Dimsum » Sun May 28, 2006 10:50 pm

{removed - see below}
Last edited by Dimsum on Sun May 28, 2006 10:57 pm, edited 2 times in total.
Dimsum
 
Posts: 6
Joined: Sun May 28, 2006 9:09 pm
Location: London

Postby Admin » Sun May 28, 2006 10:55 pm

The Rename button only becomes active when you highlight one or more files to rename.

The "New Name" column will relfect the name prior to you hitting the Rename button, so you can double-check before actually committing changes.

There's full help supplied.



Jim
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Postby Dimsum » Sun May 28, 2006 11:03 pm

Jim, my apologies. As i sent that - i realised the items should be highlighted! However I discovered another issue which I came across. I was editing the post above to instead reflect this new issue, however you replied and posted to the previous problem before I had a chance to post the new problem!

THE CURRENT PROBLEM:
================

sorry - i knew I left out something when I wrote those folder examples.

xxx_xxxx-xxxxx-xxx

to:

xxx_xxxx - xxxxx-xxx

Using the conditions you just mentioned, the problem its now doing is the following:

xxx_xxxx-xxxxx-xxx

to:

xxxx - xxxxx-xxx

so on every folder anything before the _ (including the underscore) is removed. I would like it to keep everything before the - intact.

any advice...? thanks for the help so far..
Dimsum
 
Posts: 6
Joined: Sun May 28, 2006 9:09 pm
Location: London

Postby Admin » Sun May 28, 2006 11:12 pm

I'm not sure you've going to be able to solve every situation in a single pass. Regular Expressions have a habit of getting "greedy" and taking in too much of your string.

For your second scenario you can use:

Match: ([A-Za-z]*\_[A-Za-z]*)(\-)(.*)
Replace: \1 \2 \3

(in other words, any number of upper or lower case letters, follwed by an underscore, followed any number of upper or lower case letters, followed by a dash, follwed by the rest of the string).

You could help the situation further by putting *_* in the Selection filter (Box 12) to cut down the files being shown.

So...solutions for both the examples so far. Hopefully if there's more variations on this theme then you can get the idea of the RgExp structure.

Anyway, 23:10 here, I'm off to bed. Post back if you're stuck and I'll respond in the morning.


Jim
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Postby Dimsum » Sun May 28, 2006 11:41 pm

Thanks so much for all your help Jim. Your new suggestion actually sorts out the bulk of it.

The few folders I found that caused issues are as follows:

1.If a folder had a space instead of an underscore - then it did not change:

xxxxx xxxx-xxx-xx

2.If a folder had multiple underscores before the dash then it was partially removed:

so. ABC_DEF_GHI-123-45 became _GHI-123-45 OR DEF_GHI-123-45

3.Single names before the dash were ignored:

ABC-123-45 still came out ABC-123-45 instead of ABC - 123-45

=============================================

If you have any suggestions for the above, that would be greatly appreciated. As it does sort out the bulk of my needs and has made my life a lot easier, I definitely feel you deserve a donation and I shall do that later on today. If not even for the software as least for the first class support you have provided. I can sleep happy knowing the majority of my manual renaming days are over! Thanks.. Shan
Dimsum
 
Posts: 6
Joined: Sun May 28, 2006 9:09 pm
Location: London

Postby Admin » Mon May 29, 2006 7:25 am

Hopefully you can see from my examples, though, why these few problematic folders remain.

BRU is designed for renaming en-masse (in bulk), applying the same scenario to lots of files/folders. You seem to have a situation where you've got lots of variations, as not all are the same. So you will find that you have to attack them in batches. Find the combination which solves 80%, then the combination that solves 80% of the remainder, then 80% of the remainder, etc. until you can whittle it down to a handful.

All three of your remaining examples could be solved, again using variations on the existing solution. So there's always a way - somehow :)

Let me know if there's any remaining combinations which you can't solve.

Cheers,



Jim
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Postby Dimsum » Mon May 29, 2006 3:53 pm

thanks for your reply Jim. With regards to the 3 points listed above that I have issues with, I am still unable to solve 1 & 3, any suggestions?

thanks Shan
Dimsum
 
Posts: 6
Joined: Sun May 28, 2006 9:09 pm
Location: London

Postby Admin » Mon May 29, 2006 4:38 pm

Okay, your first remaining issue (item 1) could be solved with:

([A-Za-z]*\s[A-Za-z]*)(\-)(.*)
\1 \2 \3

Your second example (item 3)...isn't this the same as where we started out? The following should work (as per my initial response)

([A-Za-z]*)(\-)(.*)
\1 \2 \3



Jim
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Postby Glenn » Tue May 30, 2006 6:30 am

Hi Jim,
This is a great product!
I'd like to make a couple of suggestions if I may.
If you use the shorthand class \w instead of [A-Za-z], you include what [A-Za-z] does but also any numbers and the underscore_. This would eliminate the two problems encountered with underscores. Initially an added bonus is it doesn't include the space character, so it wouldn't match dashes with a space around them.
To make sure it only worked with the first occurrence of the dash I used ^ at the start.
So the initial regex would be:
^([\w]*)(-)(.*)
The only problem now is that it won't match something like
xxx xxx-xxx.txt because it has a space in the name.
We can fix this by adding a space to make group 1
([\w ]).
To make it ignore the filename if it has a space before or after the initial dash (or both), you can use negative lookbehind\lookahead around group 2, which was initially just a dash (-)
Now you have this for group 2:

((?<! )-(?! ))
I know - it looks weird!

The final regex would be:
Match: ^([\w ]*)((?<! )-(?! ))(.*)

Replace:\1 \2 \3
as you suggested
It seems to match all the conditions.
It's after midnight here so I'm getting a bit punchy and may have missed something. Anyway, for what it's worth.

Cordially,
Glenn
Last edited by Glenn on Tue May 30, 2006 9:49 pm, edited 1 time in total.
Glenn
 
Posts: 28
Joined: Fri Apr 14, 2006 4:53 pm
Location: Winnipeg, Canada

Postby Admin » Tue May 30, 2006 7:40 am

Glenn,

I've been rumbled! You've noticed that I know absolutely nothing about RegExps, and blunder my way through them every time!

So, your posting will be great for the original poster, but it's equally great for me! So thank you!

RegExps are great, but they're also frighteningly complex, so I'm alway grateful when people like you (who understand them) make a posting.

I'll definitely save this! Also, I'm going to copy this thread to the RegExp forum.

Many thanks!


Jim
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Postby Dimsum » Sun Jun 18, 2006 6:29 pm

Glenn, that is just amazing! That one expression sorts out more or less everything! Thank you sooo much!

Regular expressions I am completely lost on, so its great to see you explaining it all and sorting out the problem.

thanks so much Glenn and also to Jim for all his initial help!
Dimsum
 
Posts: 6
Joined: Sun May 28, 2006 9:09 pm
Location: London

Postby Glenn » Fri Jun 01, 2007 4:32 pm

Jim & Dimsum,
I have been away from this site for a while and want to say you are very welcome. Thanks so much for the generous comments.

For all reading this post, one thing we don't want to lose sight of is that this shows what a great and versatile product Jim has created, and that we should support him if we want him to be able to continue doing this sort of work.

Cordially,
Glenn
Glenn
 
Posts: 28
Joined: Fri Apr 14, 2006 4:53 pm
Location: Winnipeg, Canada


Return to Regular Expressions