Rename PATTERN TO REGEXP with filename-passing codes

Is there a way for me to use filename-passing codes (or variables which hold their result) with Rename PATTERN TO REGEXP? I’m starting with files with names such as Image.png.b64.txt, and am trying to use a multiple-step custom command to do several things, including two renaming steps:

  1. rename "{filepath$|noext}.txt" pattern "*.b64.txt" to "*.b64"

Successfully renames Image.png.b64.txt as Image.png.b64

  1. "C:\Program Files (x86)\Gnu Core Utils\bin\split.exe" -C 1000000 -d -a 5 "{filepath$|noext}" "{filepath$|noext}."

Successfully splits Image.png.b64 into multiple files up to 1000000 bytes in size, only at line breaks, with names Image.png.b64.00000, etc.

  1. rename pattern "\{filepath\$\|noext\}\.(\d{1,5})" to "{filepath\$|noext}.\1.txt"

Hoped this would rename Image.png.b64.00000 as Image.png.b64.00000.txt, but doesn’t work. I’m figuring that either filename-passing codes can’t be mixed with regular expressions matching, or Rename PATTERN won’t operate on non-selected files when also using filename-passing codes in the arguments, or maybe the \ escape characters would need to be spliced into the result of {filepath$} beforehand as necessary then worked into the matching pattern somehow, or my usage of something else in the third command line is incorrect. Just wondering if this whole approach is a dead end and I need to look for another way.

I think the codes should work, but you wouldn't need to escape them, as the parsing is done before the argument gets given to the regex functions.

Try something like rename pattern "{filepath$|noext|escregexp}\.(\d{1,5}) etc. The escregexp modifier will cause any regex chars in the filename to be escaped automatically.

Thanks, looked promising, but…

rename pattern "{filepath$|noext|escregexp}\.(\d{1,5})" to "{filepath$|noext|escregexp}.\1.txt"

…still has no effect. In my test case, the escregexp probably isn’t needed in the replace string, so I tried it both ways. No go either way.

I think the PATTERN argument doesn't like the path info. I'd use {file$} instead of {filepath$} and move the path part to FROM.

@nodeselect
@nofilenamequoting
Rename FROM="{filepath$}" PATTERN="{file|noext}\.(\d{1,5})" TO="{file|noext}.\1.txt" REGEXP

The quotes and @nofilenamequoting are probably not needed.

Thinking about it there's no real need to use {file} in the pattern at all. It's a wildcard, it can match anything :slight_smile:

PATTERN="(.*)\.(\d{1,5})" TO="\1.\2.txt" is probably all you need.

2 Likes

Unless I’m missing something here, I don’t think that’s going to work, since {filepath$} itself isn’t what’s being renamed in the second Rename operation. To recap and paraphrase from my first post:

  1. Image.png.b64.txt (for example) is selected

  2. Image.png.b64.txt gets renamed to Image.png.b64, and should remain selected by modifier @nodeselect

  3. Image.png.b64 gets chopped into multiple pieces with names Image.png.b64.00000, etc.; Image.png.b64 should remain selected

  4. Image.png.b64.00000, etc., (hopefully) get renamed as Image.png.b64.00000.txt, etc.; Image.png.b64 should remain selected

That’s the problem — there will often be other potential matches if I DON’T use {file} or {filepath}, which I don’t necessarily want matched. I only want files whose names are based on the original selected file(s) matched.

I’m trying to come up with a way to discern this, but does it matter for me that the original selected file(s) got renamed in the first step of the command? Does that change what {filepath$} in the second rename step (third command step) refers to, or does it still refer to the original selected filenames? I tried using {dlgchoose|{filepath}|Ok} or @confim:{filepath} between the second (Split) and third (second Rename) steps, but I ended up getting both dialogs before the Split command.

Most recent attempt variation:

@filesonly
@nodeselect
@leavedoswindowopen
@set filepath={filepath$|noext}
@set filepathre={filepath$|noext|escregexp}
dopusrt /acmd rename from "{$filepath}.txt" to "{$filepath}"
"C:\Program Files (x86)\Gnu Core Utils\bin\split.exe" -C 1000000 -d -a 5 "{$filepath}" "{$filepath}."
dopusrt /acmd rename from "{$filepathre}\.(\d{1,5})" pattern "{$filepathre}\.(\d{1,5})" to "{$filepathre}.\1.txt" REGEXP

Still doesn’t rename the perform the second rename (.b64.00000 to .b64.00000.txt, etc.).

rename from "{$filepathre}\.(\d{1,5})"

The Rename FROM argument (for specifying which files are to be renamed, instead of using the current selection) can take wildcards but not regex, so that won't work.

(The PATTERN argument can work with wildcards, but not FROM.)

This all seems complex enough that using a script probably makes more sense. Then you have more control over which filenames are passed to what, and can have logic like "if file X exists then see if file f(X) also exists and do things to if" where f() is some kind of transformation on the filename.

With that in mind, I tried replacing from "{$filepathre}\.(\d{1,5})" with from "{$filepath}.*", as that would most likely take care of everything I’d need to throw at it without affecting any other files (full code as shown):

@filesonly
@nodeselect
@leavedoswindowopen
@set filepath={filepath$|noext}
@set filepathre={filepath$|noext|escregexp}
rename from "{$filepath}.txt" to "{$filepath}"
"C:\Program Files (x86)\Gnu Core Utils\bin\split.exe" -C 1000000 -d -a 5 "{$filepath}" "{$filepath}."
rename from "{$filepath}.*" pattern "{$filepathre}\.(\d{1,5})" to "{$filepathre}.\1.txt" REGEXP

But I get An error occurred renaming ‘{$filepath}.txt’: The system cannot find the file specified. (2)

Also tried using dopusrt /acmd, with all other code same as above:

[…]
dopusrt /acmd rename from "{$filepath}.txt" to "{$filepath}"
[…]
dopusrt /acmd rename from "{$filepath}\.*" pattern "{$filepathre}\.(\d{1,5})" to "{$filepathre}.\1.txt" REGEXP

But that opens the Advanced Rename dialog, twice, and does nothing else.

Had a feeling this was going to go there. :slight_smile:

But by default the Rename command will only work on files that are selected. I think you're making this more complex than it needs to be.

Willing to give this a try? The split.exe's parameters are a guess - might need a bit of adjustment.

@nodeselect
@nofilenamequoting
Rename FROM="{filepath$}" PATTERN="*.*" TO="*"
"C:\Program Files (x86)\Gnu Core Utils\bin\split.exe" -C 1000000 -d -a 5 "{filepath$|noext}" "{filepath$|noext}."
Rename FROM="{filepath$|noext}.*" PATTERN="{file|noext}\.(\d{1,5})" TO="{file|noext}.\1.txt" REGEXP

But as lxp kindly brought to my attention, FROM is supposed to change that behavior. The help file says (in the section on PATTERN): “Specifying the FROM argument as well will mean the command ignores the current file selection and applies the specified rename on all matching files.”

Well, I tried, anyway. :slight_smile:

Sure, I’ll try anything once. About the equals = signs, though: I assume that’s just an optional convention that you like using? The examples in the help file’s Rename section don’t use them.

That part was already working for me, at least for my attempts in which the first Rename successfully completed, but guess what? You nailed it. The whole multi-step command worked correctly with one *.b64.txt file selected and two others present, and then with all three selected. Thanks, lxp.

Yes but why are you using FROM at all?

Select the files you want to rename, run Rename on them without FROM, and those are the files that will be renamed.

To save myself work in the long run, of course. In combination with the other steps in this command, this workflow is something I’ve been doing manually on a regular basis, involving a fairly large amount of files, for a while. And I want the original *.b64.txt files to remain selected because my next step is to move, delete, or do something else with them.

Thanks to all for advice and suggestions.