Rename all files in a folder using regular expression with one-click

I have a toolbar which is laughingly simple by the standards of regulars here (I'm sure) but is amazingly helpful to me and makes my file manager experience a joy. Because my needs are simple, too. It looks like this :


I'd like to add a click to that drop-down list (or a new button), with what follows...

I have a common problem with many files.
They all have a name format that I'd like to change.
Via Google, I found a solution using regular expressions (provided by Einstein's descendent, Leo), as shown in this image :

All the target files have the same format : nn-nn OR nn-n
They represent the display sequence of pages.
left-most (incorrect) nn corrupts the right-most nn | n(correct) ones.
To remove the left-side nn, I run the shown regular expression and it works fine.

Done and dusted.
Then I just do another batch rename of page # 1-9 to make them 01-09.
That works fine too.

They are page numbers for digital books ; now, the pages are in the correct order for reading !
Now I have the files ordered correctly in the folder.

But, given that hundreds of current folders are involved, and more will be involved later, I'd like a simple solution to add the 2 regular expression statements without having to type them again for each folder (Directory Opus has spoiled me and made me lazy).

Actually, each folder is an unpacked zip file. I'm sure it would be impossible to start with a zip, perform the file (page number) renames, and end up with a zipped folder, all by clicking on 1 button on my toolbar.

As said, the same conversion is applied to every folder (zip). Just delete the left 2 digits and dash, and add a zero to any single page numbers on the right side.

How can I automate that partly or fully?

Unfortunately, I'm as dumb as a box of rocks.
It took me a day to even get a vague understanding of the above regular expression method.

Please help... but be kind.
I'm not a guru... I'm ancient and, in my youth, it was all about papyrus rolls.
:slight_smile:

Batch renaming inside zip files is technically possible, but it is often very slow, unless the archives are quite small.

(Each file is renamed as a separate operation behind the scenes, and with a zip archive that means re-writing the archive, once per file being renamed. In the future we might make it so they can all be done in one operation, but we haven't yet. It's quite rare to do batch renames in zips, and quite a lot of work to improve, so it's remained at a low priority for a long time.)


To avoid having to type the regex every time, save it as a preset. That will add it to the list on the left. The save icon (2nd icon above the list of presets) lets you save a new preset.

Presets will also automatically be listed in the default toolbars, under the Rename button's menu. If you're using custom toolbars, add these two commands to the menu of your choice, with a separator between the two, to get the same thing:

List favorite presets:

Rename PRESET=!list,favesonly
@nodeselect

List all the other presets:

Rename PRESET=!list,nofaves
@nodeselect

Focusing on doing the renames with unpacked archives (i.e. normal files and folders), if you go to the parent folder, select all the folders containing the files, you can then apply your regexp to all the files within those folders. All you have to do is turn on Rename files in selected sub-folders.

I recommend also turning on Show preview of sub-folder contents and checking the preview to make sure it isn't going to rename any files you don't expect it to rename.

But leave Rename folders as well as their contents off.

You may also want to tighten up the regular expression so it is less likely to match unexpected files. For example, this will match everything in your example screenshot, without matching much else:

^(\d\d)-(\d\d?[A-Z]?)$

If you are using a rename preset, remember to save it again after changing the regex.

To clarify, you want to take part1-part2 and rename it to part2 padded to length 2 with zeroes?
I think you need to use a macro

function pad(num, size) {
    var s = num+"";
	while (s.length < size) s = "0" + s;
	return s;
}

function OnGetNewName(getNewNameData)
{
	var re = new RegExp("^(\d\d)-(\d\d?[A-Z]?)$");
	if(re.test(getNewNameData.item.name_stem))
	{
	  return pad(getNewNameData.newname_stem.replace(re, "$2"), 2) + getNewNameData.newname_ext;
	}
}

The second part of your question. How can I do this to a zip. The rename command will work on zips, however it might be slow, each rename will require the zip be rewritten. I think you would be better extracting the zips make changes then re-compress.
Are all the the zips in one folder?

  • extract all zips to child folders
  • enter flat view mode
  • rename all the image
  • exit flat view
  • rezip folders.
    Prob best to back up first :wink:

Edit, Updated, to use @Leo's regexp. I started before he posted, but finished after...

1 Like

Leo, wowbagger... thank you in 100 languages !
:slight_smile:
It all flew past me, I'm afraid... but that's ok... there are actually people dumber than me !
I need to do a lot of understanding but that will help me make other solutions in future. It's a challenge !
Start with baby steps... end with giant steps for all mankind.
:slight_smile:
It will take me a while (I hope days rather than months) to properly grasp what you guys are saying.
But nothing really worthwhile is easy.
I'll plug away at it now you've given me the cue words to study.
Many thanks to you both.
:slight_smile:

1 Like