Keep the x first characters

I need to create button to rename files and keep the x characters (dialog prompt) from filenames.
It's maybe easy but I failed.

You'll want to read up on the regular expression syntax in Opus... here's one way to get the first 4 characters of an existing filename in the advanced rename dialog:

Old name: (^....)(.*)
New name: \1

The (^....) part matches the first 4 characters from the beginning of the filename (might not actually need the ^ in this case - but it otherwise forces the character capture to start from the beginning of the filename which you might need to keep if you have filenames with things like spaces in between 'parts' of the filenames). The 'parts' of the filename your regex will capture is separated by the () bracketed groups. So with (^....) being the first group, you get to access whatever characters were matched in the "New name" string as \1. This example old name also has a second group (.*) which basically captures everything AFTER the first 4 chars captured by the first group.

Shout for any other help... I'm sure there are other ways to do it - such as using a rename SCRIPT, etc.

Thanks but my problem is in fact, is it possible to have button with regexp like this one...

@nodeselect @set debut={dlgstringS|Rogner les x premiers caractères...|< Aucun >} Rename REGEXP PATTERN ".(.+)#{$debut}" TO "\1" AUTORENAME TYPE=dirs Rename REGEXP PATTERN ".(.+)(\..*)#{$debut}" TO "\1\2" AUTORENAME TYPE=files

...but to keep x first characters.

If not I will create vbscript.

The obvious solution would be to set a variable (as you're doing with "debut"), and pass that value to a regular expression to capture the first {$debut} characters:

^(.{{$debut}})

However, this doesn't work, as there does not appear to be a way in DOpus to nest a DOpus variable reference inside curly brackets. See:

https://resource.dopus.com/t/auto-move-grouped-files-into-own-folders/14124/1

for the same issue discussed.

If you'll use it, I'll add this functionality to my Dynamic Renamer script. Currently it supports trimmer the first or last N characters. I can make an option to trim all but the first or last N characters.

.{n} will match n characters.

There's a slight problem with using "{" and "}" around "{$debut}" here, but it can be worked around by using variables for the { and } themselves.

So, like this:

@nodeselect @set brace1={ @set brace2=} @set debut={dlgstringS|Rogner les x premiers caractères...|< Aucun >} Rename REGEXP PATTERN "^(.{$brace1}{$debut}{$brace2})" TO "\1" AUTORENAME TYPE=dirs Rename REGEXP PATTERN "^(.{$brace1}{$debut}{$brace2}).*(\.[^.]+)" TO "\1\2" AUTORENAME TYPE=files

Clever!

I'll update the other post to reference your nice solution.

Of course :laughing:
Many thanks guys

Back from vacation...
I try this regexp but failed...

^(.{3}).*(.[^.]+)

What's happen ?

Please give us more context.

Tried it with which files, and which command?

Failed in what way?

Sorry...
Your last script button don't work on file or folder...
So I try this regexp in rename dialog on file "Nouveau Document texte.txt"

Old: ^(.{3}).*(.[^.]+)
New: \1\2

But nothing happen...

Like this?

Make sure Type was set to Regular Expressions.

Also make sure Preferences / Miscellaneous / Advanced: regex_style is set to C++ TR1 (ECMAScript) and not Legacy Opus 9 Style.


I had "Legacy Opus 9 Style".
There will be no problem with my old regexp ?
Thanks

[quote="AlbatorV"]I had "Legacy Opus 9 Style".
There will be no problem with my old regexp ?[/quote]

There might be. There are some differences (else we wouldn't need the option). Presumably you turned on the legacy mode for a reason at some point, but I can't guess if the reason still applies to any of the regexps you still use as I don't know what they are.