GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Variable in regexp renaming


#1

Hi all,

I have a rename command for removing the first n characters of a file name:

Rename REGEXP PATTERN=".(.*)#{dlgstring|Remove first x characters:}" TO="\1"

Works like a charm. :wink:

Now I want to rename leaving the first n characters.
In this case, using '#' for repetition is not an option because I don't know how many characters to remove. Instead, I thought I'd use the regexp quantifier {} to define how many characters to leave. With a fixed number of eg. 7 characters the command would look like this:

Rename REGEXP PATTERN="(.{7}).*" TO="\1" TYPE=dirs Rename REGEXP PATTERN="(.{7}).*(\..*)" TO="\1\2" TYPE=files

But I can't figure out how to make it work with a variable number:

@runonce:@Set count={dlgstring|Leave first x characters:} Rename REGEXP PATTERN="(.{{$count}}).*" TO="\1" TYPE=dirs Rename REGEXP PATTERN="(.{{$count}}).*(\..*)" TO="\1\2" TYPE=files

This doesn't work (it leaves only the first character), presumably because of the nested curly brackets.:cry:

Any ideas?
(Yes, of course I could use scripting, but I hope it's not necessary...)

Thanks a lot,
MartO


Dialog in a rename script
Dialog in a rename script
#2

{{ will turn into a literal { but you still need an extra { for the {$count} sequence to be replaced by the value (otherwise you'll end up with a literal {$count} in the command.

So change both lines to {{{$count}} and it should work.


#3

Thanks jon,

that makes sense, but it still doesn't work. :frowning:

@runonce:@Set count={dlgstring|Leave first x characters:} Rename REGEXP PATTERN="(.{{{$count}}).*" TO="\1" TYPE=dirs Rename REGEXP PATTERN="(.{{{$count}}).*(\..*)" TO="\1\2" TYPE=files

Now the command makes the the file "disappear" by literally renaming it to a file called "2" in the folder "\1". This seems to happen whenever there is a mistake in the regexp pattern...
I wasn't sure if you meant {{{$count}} or perhaps {{{$count}}}, so I tried both...

Thanks,
MartO


#4

As someone who has never managed to fully get to grips with RegEx, I fall back on Mr C's excellent Dynamic Renamer for this sort of thing.
I just checked and it can do exactly what you want.


Regards, AB