How do I do these renames in scripting mode?

I have the following rename operations:

"_" to " " findrep
"(.)[.](.)#" to "\1\2" regexp
"^( +)(.
)( +)(..)" to "\2\4" regexp
"^( +)(.
)" to "\2" regexp
"(.)(\ )(..)#" to "\1\3" regexp

In order these are for turning _ to space, removing brackets and text inside them, removing extra spaces in the end of the filename and removing extra spaces in front of the filename. I'm sure there are smarter ways to do some of these but these have worked fine.

Earlier I have had these set as two buttons (left and right click on single button) because having all that stuff in one button doesn't work properly. Now I'd like to use scripting mode to do them.

Basically what I want to do is a) remove underscores, b) remove brackets and all stuff inside them, c) remove extra spaces.

How would I do these in scripting mode?

Have you had a look at the Rename Scripting forum, specifically the Titlecase and multiple Regular Expressions examples?

They should be a good starting point for a script like this. (The multiple Regular Expressions example is intended to be used as the basis for exactly this kind of thing.)

The only obstacle is to convert the regexp syntax into the one which VBScript uses but that usually just means turning \1, \2, etc. into $1, $2. Be sure to test your script in a safe place first, just in case, though. :slight_smile:

Yeah I got it working now. One thing that still kind of bothers me is this:

File name is something like "[252542AFBB] blaablaablaa [CDBR456272].avi"

re.Pattern = "[.*]" doesn't work properly, matches everything and returns empty line when set to replace with ""

re.Pattern = "[.](.)[.*]", replace with "$1" on the other hand does work but I was kind of hoping to have a regexp that removes brackets no matter where they are in the filename.

Any idea what I'm doing wrong here?

[quote="kasakka"]
re.Pattern = "[.*]" doesn't work properly, matches everything and returns empty line when set to replace with ""[/quote]
I think you just need to put the part of the expression that you want to extract in brackets. \1 (or $1 in VBScript) will turn into the first thing in brackets, but in the pattern you have there isn't anything in brackets so the \1 (or $1) turns into nothing.

i.e. instead of

[.*]

try

[(.*)]

Let us know if that doesn't work. Maybe I've misunderstood what you want the expression to do.

[quote="kasakka"]Yeah I got it working now. One thing that still kind of bothers me is this:

File name is something like "[252542AFBB] blaablaablaa [CDBR456272].avi"

re.Pattern = "[.*]" doesn't work properly, matches everything and returns empty line when set to replace with ""

re.Pattern = "[.](.)[.*]", replace with "$1" on the other hand does work but I was kind of hoping to have a regexp that removes brackets no matter where they are in the filename.

Any idea what I'm doing wrong here?[/quote]

You are getting trapped by greedy vs non-greedy regular expressions.
Not tested (I've still only got DOpus 6 on another machine :frowning:) but in most versions of regular expression putting a "?" after should make it find the nearest bracket so both bracket ranges in the above should get replaced by null.

ie. try
re.Pattern = "[.*?]" replaced by ""

I think Alec's right. I didn't notice the other pair of brackets! (It's been a long day.)

If the ? in "[.*?]" doesn't solve it (I don't know if it will work or not, regexp syntax varies so much from program to program, so give it a try) then an alternative might be something like this:

[[^[]*] <-- EDIT: This is rubbish! See Enigma's correction below.

That, if I haven't messed up the regexp due to lack of sleep, should match a [, then as many characters as possible -- so long as those characters don't include a second [ -- until a ].

(Random sleepy thought: Unicode should have special regexp characters so we don't have to escape normal characters all the time. Except when dealing with meta-regexps, which would probably drive a person insane anyway so aren't worth worrying about! Maybe Unicode already has such characters. There's some pretty weird stuff in there already.)

Hm, I guess you mean

\[[^\]]*\]

Which is

\[ - opening bracket [^\]]* - characters that aren't a closing (!) bracket \] - closing bracket
Greetings,
Jan


  1. ↩︎

Yes! Thanks for the correction, it's absolutely right.

No more regexp for me until I've had some sleep! :slight_smile:

Well - good night then! :slight_smile:

I know how you feel - I'm getting by far too little sleep myself at the moment. Yesterday I spent more than an hour investigating a CSS error in a web page I'm creating, and the solution was more than obvious - I just had forgotten about a setting I had written into a second CSS file... :unamused:

That's the moment to stop working and take a nap, no matter how urgently you have to finish a project... :wink:

Greetings,
Jan

[quote="Alec_Burgess"][quote="kasakka"] .... One thing that still kind of bothers me is this:

File name is something like "[252542AFBB] blaablaablaa [CDBR456272].avi"
....
[/quote]

You are getting trapped by greedy vs non-greedy regular expressions.
...

ie. try
re.Pattern = "[.*?]" replaced by ""[/quote]

If the above works you probably want to change it to:
re.Pattern = "\s?[.*?]\s?" replaced by ""[/quote]

This would ALSO get rid of leading or trailing space before/after the bracketed string. (This use of "?" in \s? is different than the .? - it matches zero or one of the "thing" before it. The ? after .? converts a normally greedy match to non-greedy.

I HEART regular expressions :stuck_out_tongue:

Actually, none of the suggested patterns will work with nested brackets like:

New [Te[x]t] [Do[um[e]n]t].txt

and it will not give exact results, as the pattern should allow removing outer brackets including any inner ones, and give "New .txt" as a result.

So, the following pattern should be used:

\[[^\]]*[^\[]*\]

Regards,
TheTruth

Flogging a dead horse department: 99% of readers should delete and move to next post!

[quote]Actually, none of the suggested patterns will work with nested brackets like:
Code:
New [Tet] [Do[um[e]n]t].txt

and it will not give exact results, as the pattern should allow removing outer brackets including any inner ones, and give "New .txt" as a result.

So, the following pattern should be used:

Code:
[[^]][^[]][/quote]

Your match is better than mine but doesn't quite catch everything.
eg.

 [code]New [Te[x]t] [Do[um[asdf]===[fdsa][e]n]t].txt[/code]

gets changed to: New ===.txt
It makes four matches in the above:

[Te[x]t] [Do[um[asdf] [fdsa] [e]n]t]

but considers the "====" to not be bracketed at all.

Note: for testing I changed your expression to:

[code] [\[\(][^\]\)]*[^\[\(]*(\]|\))[/code]

same thing but it considers both square and round brackets to be identical

PCRE regular expressions have a concept called recursive matching.
I suspect this is the engine that DOpus 9 is using?

This expression matches arbitrarily deeply nested round parethesis:

[code] \(((?>[^()]+)|(?R))*\)[/code]

or adjusting it as above to handle [ & ( and ] and )

 [code][\(\[]((?>[^()\[\]]+)|(?R))*[\)\]][/code]

so my counter-example "New [Tet] [Do[um[asdf]===[fdsa][e]n]t].txt" ==> "New .txt"
Note if the brackets are deliberately unbalanced by tacking an extra "]" on the end we get:

"New [Te[x]t] [Do[um[asdf]===[fdsa][e]n]t]].txt" ==> "New ].txt"

Testing was done in Notetab 5 (see section Recursive Patterns in Regex.chm if you happen to have it) or on the web check:
Wikopedia: http://en.wikipedia.org/wiki/PCRE#_note-0
which points to: http://www.pcre.org/pcre.txt In that:
Search for "RECURSIVE PATTERNS"

I'd certainly never be able to code the above of the top of my head, and had never attempted it until this thread caught my attention!

Hm, I guess you mean

\[[^\]]*\]

Which is

\[ - opening bracket [^\]]* - characters that aren't a closing (!) bracket \] - closing bracket
Greetings,
Jan[/quote]

This is what I was after and works great for my purposes. Thanks to everyone!


  1. ↩︎

@Alec_Burgess

Yes, I know. Actually this is all I had come up with out of my head before my head blew up.

[quote]This expression matches arbitrarily deeply nested round parethesis:
Code:
(((?>[^()]+)|(?R))*) [/quote]
I gues that won't work in a VBScript/JScript, as I may remember the RegExp class used by the scripting engine allows only Positive/Negative Lookaheads (?=...)/(?!...) beside Backreferences (\1,\2,...), but I may not be right.

Regards,
TheTruth