Help with a rename command

This is my first post, so I'd like to say 'Hello' :slight_smile:

First of all, let me say that I am just amazed by Dopus capabilities, as well as how detailed is the manual.

And here comes my 'problem':

Below is the Rename command I am using quite often:

@Set var = {dlgstring|How many symbols do you want to delete?} @Set var2 = {dlgstring|What do you want to replace it with?} Rename PATTERN "(.{{{{{$var}})(.*)" TO "{$var2}\2" REGEXP

It works fine except one thing. It ignores 'space' character at the end of var2. For example, instead of 'tobedeletedTheRest->Something - TheRest' I get 'tobedeletedTheRest->Something -TheRest'.

I tried using \s for the missing space and accidentally discovered the way to put files I rename to new folders :wink:
Anyone has any ideas?

I tested you code and it didn't do what I expected. To simplify the the discussion I believe this code below highlights the issue you are having.
It will create a folder with the name you entered with 'pad' on the end (I don't think you can create a folder that ends in a space, hence the adding of pad).

@set name {dlgstring|Enter new folder name} CreateFolder "{$name}pad"

If you enter "test" you get a folder called "testpad", if you enter "test " (note the space) you get "testpad".
Looks like dlgstring trims the user entered value. I can understand why it does this and I don't think you can disable the trim. Least there is nothing here.

You could request a no trim option/flag/version to be added. Leo?
Another option would be to have a special char that you look for and replace with a space. like if you enter "|||" replace it with a single space, so "test|||" would become "test ". You might need to use scripting to get that to work.

Place your var2 inside the RE and end or surround the value you enter in the dialog with a terminator character. The RE will need to be adjusted to not capture the terminator.

It's not {dlgstring} that trims the string, it's @set.

Since the variables aren't needed in this case, and are just being used for clarity, you can move the {dlgstring} codes into the Rename command to solve the problem:

Rename PATTERN "(.{{{dlgstring|How many symbols do you want to delete?}})(.*)" TO "{dlgstring|What do you want to replace it with?}\2" REGEXP

(That should be all on one line.)

Thank you for your replies.

@wowbagger:
As for myself, I can get by subsequent 'find and replace' as this action does not remove entered spaces. However, I dont have any experience with scripting, so I would not know how to make it an one-step operation.

@MrC:
As I've written above, I am an inexperienced user, so I don't know how to make use of your advice.

Thanks again,
addict

Thank you Leo, it works great!

Sorry about not giving you enough to be helpful last night. I was responding on an iPad late at night.

Something to consider - if you have to manually count how many characters to remove, you might be artificially limiting the utility of your button, or working too hard. There are probably more efficient methods, but they will obviously depend on your needs. If you're finding your solution doesn't meet your needs, don't hesitate to ask for alternative or better ways to do things.

Counting the letters is fine for now. Some day I'll learn more about regex, esp. word boundaries could be useful here, I think.

Cheers.

Im looking for a similar command.

This one only addresses number of characters from the start.

How to address say characters from 4 to 8 and replace with x ?

Anyone can help with this?

@addict I agree. Counting the characters is faster than figuring out regexp :stuck_out_tongue:

Btw I found this code from here Rename Toolbar v3 (english and french) but is addresses only single character to replace.

[code]<?xml version="1.0" encoding="UTF-8"?>
<rename_preset autorename="yes" case="none" script="yes" type="normal" typeval="0">



</rename_preset>[/code]

You really can't do these renames flexibly and easily without having a little knowledge of regular expressions. The reason is simple - regular expressions were designed to be a simple language for describing patterns; once you learn even a little about this simple language, you now have the power to solve all sorts of problems like this without thinking too much or relying on others to debug your scripts.

Your question is how to remove chars 4 through 8. That can also be stated, how do I save characters 1-3 and 9-n. And in RE's, that's Easy:

Old name: ^(....).....(.*)$
New name: \1NEW_STRING_HERE\2

Old name can be more "readable" in terms of eliminating dot counting:

Old name: ^(.{4}).{5}(.*)$

Just knowing the basics for the above expression gives you as much power as needed for many simple renames.

Writing and debugging a long script to do a simple rename seems much harder than learning a few basic rules. Folks are afraid of REs because the language looks some symbolic. But so does Chinese or Russian to me. Once you become family with a few characters and rules, it isn't very intimidating.

You might consider trying out my Dynamic Renamer. It eases many rename tasks which are often more complicated due to the limitations in the DOpous rename tool, or which require scripting. The tool was written having folks like you in mind, which often want to do simple transformations and not be bothered by crafting complex regular expressions and scripts.

Your questions above, however, makes me consider a nice feature to Dynamic Renamer. It would allow you to specify a range of characters for which you want a replacement made. With that, you can say something like exchange chars 4-9 with the string XYZ. If that would be useful, I'll add it.

MrC, that would be yet another useful addition to your excellent utility.

Regards, AB

Version 1.10 is posted.

@OpelOpus - use this transformation in Dynamic Renamer (version 1.10) to replace characters 4 to 8 with X:

-rr/4-8/X/

That's all there is to it. You can enter that value as the New name, and save this as a preset, and VoilĂ , instant script. You can call this up from a button, and even use dialogs to fill in the values like the range and replacement string.

:thumbsup: Awesome mr.C! IT WORKS

For the occasional renamer like me this covers a lot of my rename jobs.

Just need this with a dialog and I'm good to go.

How do I go about using this in a button with dlg string. I tried something like this Rename PATTERN (.*) TO "-rr/{dlgstring|Enter character string to replace...}/{dlgstring|Replace with...}/" AUTORENAME but that didn't work.

Thanks again mr.C. Your work and help is much appreciated.

You're welcome.

That won't work as a TO argument. You need to use the PRESET and ADVANCED options to get the script preset called up, and open the Advance Rename dialog. I don't think you can use the FROM and TO to pass initial values when using those options.

You'll have to ask for an enhancement request for this.

No problem! This works Rename PRESET="Dynamic Renamer 1.10" ADVANCED
I don't necessarily need it with dlg string. the -rr/x/x/ is so short to type that it doesn't need dlgstring.

If you consider improving the the rr argument, I'd suggest also making it from the end. Meaning. replace string from the end.

I could use the -K (kill tekst before). It doesn't have option to replace, but thats just fine. I could do that in a second rename pass with -^ (append) argument.

Thanks mrC!. Great tool.

I've added it for the next update. In the mean time, you can replace the block of code

elsif ($opt =~ s#^-rr/([^/]+)/(.*?)/##) { # range m-n replacement my ($range, $repl) = ($1, $2); if ($range =~ /^(\d+)(?:-(\d+))?/ and $1 > 0 and (defined $2 ? $2 >= $1 : 1)) { substr $_, $1 - 1, $2 ? $2 - $1 + 1 : 1, $repl; } }

with

elsif ($opt =~ s#^-(rr|RR)/([^/]+)/(.*?)/##) { # range m-n replacement my ($mode, $range, $repl) = ($1, $2, $3); if ($range =~ /^(\d+)(?:-(\d+))?/ and $1 > 0 and (defined $2 ? $2 >= $1 : 1)) { substr $_, $mode eq 'rr' ? $1 - 1 : $2 ? -$2 : -$1, $2 ? $2 - $1 + 1 : 1, $repl; } }

And the new comment at the top of the file will be:

-rr replace range; format -rr/<range>/newstr/, rr=front, RR=rear; replace range of chars with newstr, <range> is either m or m-n and m <= n

Splendid mate!
Not sure what I did wrong but seems like I can't even copy-paste a code properly in a file. No worries though. I'll wait for your next update.

I've updated Dynamic Renamer to version 1.11, and it includes the -RR option to range replace at the end of the filename.

You'll need to update a perl module. See the Note in the first post of the Dynamic Renamer thread.