Filename Cleanup Regex

I've searched these forums quite hard looking for the solution to the following problem, and I'm close but with no cigar. I've hacked together a few solutions from other people but haven't really gotten to my goal.

What I would like to achieve is something as so:

1- Strip periods from filename
2- strip underscores from filename
3- convert filename to Title Case
4- Insert periods between numbers and the letter V or v
5- Convert all instances of a hyphen - into a hyphen flanked by a space on either side.

This would convert the following:

FILE.NAME.V.1.2.3-Temp.rar

into:

File Name V1.2.3 - Temp.rar

(Note: the above is the ideal output. V.1.2.3 is acceptable but if it can be worked out that would be lovely)

Doing this to a selection of files is a luxury, being able to do it to just the selected file I will be happy with.

Included is the code for the 2-button combo I have so far come up with. It sometimes works, but the double-space removing FINDREP fails if there are no instances of a double space. If I could have it fail quietly, that would also be OK.

<xml> <button> <guid>{417C2F43-62E1-4318-BA99-C354D2D63F0F}</guid> <label>Strip periods</label> <icon1>84</icon1> <button> <guid>{417C2F43-62E1-4318-BA99-C354D2D63F0F}</guid> <label>Strip periods</label> <tip>Strips periods between alphabet characters only</tip> <icon1>84</icon1> <function> <instruction>rename PATTERN="." TO=" " FINDREP CASE=Allwords</instruction> <instruction>rename PATTERN="-" TO=" - " FINDREP</instruction> <instruction>rename PATTERN=" " TO " " FINDREP</instruction> <instruction>rename PATTERN=" " TO " " FINDREP</instruction> <instruction>@nodeselect</instruction> </function> </button> <button> <guid>{F5A5F1ED-8165-4127-AFA7-3E6C28046D1D}</guid> <label>Number Spaces to Dots</label> <icon1>16</icon1> <function> <instruction>rename PATTERN="(.*[Vv0-9]) ([0-9].*)" TO="\1.\2" REGEXP</instruction> <instruction>rename PATTERN="(.*[Vv0-9]) ([0-9].*)" TO="\1.\2" REGEXP</instruction> <instruction>@nodeselect</instruction> </function> </button> </button>

Thanks in advance if any Einstein out there can crack it!

OK denyerec,

I think John and I both need several examples of filenames you wish to convert, and the converted resultant name.

The simple fact is I haven't had much time to think about this properly.
Perhaps in a few days or so.
I think John and I are still hoping a RegExp solution can be found.
Would a two button solution be OK ?

That failing, we could go for a command line solution .......... perhaps in Sync: with Tanis' Titlecase Rename.

Regards,
Porcupine

I went for the Full Monty here and attempted to do it all with a number of selected files. It only requires 10 commands!

The Bad News:

I don't think you can safely perform this as a single-click operation. (Maybe somebody else can figure that out.) I tried combining all of the commands into a single button to use on a single selected file. It simply doesn't work because Opus launches the next command before the previous one has time to finish. You would see dialogs stating that a file could not be found. But that dialog is for one command that failed, and others are still launched afterward. The net result is that the files are named all out-of-whack. I think Leo calls this a "Race State".

The Good News:

It can be safely and reliably done with multiple files selected! You just have to be patient and wait for each step to complete. What I did is select a number of files and scroll down to where the last few on the selected list were displayed. Opus works from the top down in the sort order. When you see the last file name change, wait a few more seconds, as some files are still being updated and written to the hard drive. If you have a hard drive with a large cache, wait a little longer. Then move on to the next step.

Test Case Files: v5.0[ul][li] 0-A_File.012.With a Name v.0.5.1.Fun.719.TXT[/li]
[li] 006-Some_File.672.With another Name.v.6.5.7.Not_so_much_Fun.779.TXT[/li]
[li] 02-Some_Other_File.232.Yet another name.v_2_5_3.Fun_for_all.739.TXT[/li]
[li] 03.Yet.Another.File.342.Some Name_v.3.5.4.All.for.Fun.749.TXT[/li]
[li] 080-File_Me.892.Name Me v_8_5_9.Funny.799.TXT[/li]
[li] 1-File_away.122.My Name.v.1.5.2.5.6.Funky.729.TXT[/li]
[li] 40-File_File_File.452.Name Name-v-4-5-5.Fun_no_more.759.TXT[/li]
[li] 50-File_cabinet.562.Name of drawer.v.5.5.Fun_for_the_whole_family.769.TXT[/li]
[li] 700-File_your_nails.782.Name _ your _ dog.v.7.Fun_with_friends.789.TXT[/li]
[li] A____file___with....aversion to a........version.TXT[/li]
[li] Correctly Named File v1.2.3 - 1000.txt[/li]
[li] FILE.NAME.V.1.2.3-Temp.TXT[/li][/ul]Results:[ul][li] 0 - A File 012 With A Name v0.5.1 Fun 719.txt[/li]
[li] 1 - File Away 122 My Name v1.5.2.5.6 Funky 729.txt[/li]
[li] 02 - Some Other File 232 Yet Another Name v2.5.3 Fun For All 739.txt[/li]
[li] 03 Yet Another File 342 Some Name v3.5.4 All For Fun 749.txt[/li]
[li] 006 - Some File 672 With Another Name v6.5.7 Not So Much Fun 779.txt[/li]
[li] 40 - File File File 452 Name Name v4.5.5 Fun No More 759.txt[/li]
[li] 50 - File Cabinet 562 Name Of Drawer v5.5 Fun For The Whole Family 769.txt[/li]
[li] 080 - File Me 892 Name Me v8.5.9 Funny 799.txt[/li]
[li] 700 - File Your Nails 782 Name Your Dog v7 Fun With Friends 789.txt[/li]
[li] A File With Aversion To A Version.txt[/li]
[li] Correctly Named File v1.2.3 - 1000.txt[/li]
[li] File Name v1.2.3 - Temp.txt[/li][/ul]Attached is an Opus toolbar file with the Ten Steps to File Name Bliss and a .zip file containing the test cases I tested it with. Each button's info tip tells you what it does. Each test case file contains the expected resulting file name, after all steps have been completed one-at-a-time in sequence.

v5.0 Toolbar Features:[ol][li] Following John's example, I have created two all-in-one buttons that each perform all of my 10-step process: [ol][li] Clean 1 Filename - Use this button on one and only one selected file. If you try to use it on more, you will receive error dialogs and your file names will become disheveled. This buttons works fairly fast, and meets 100% of the stated requirements, plus a couple I added. It did not trigger dopus.exe to consume 96+% CPU utilization during my tests on the test cases listed above.
[/li]
[li] Clean +1 Filenames - Use this button on 1 or more selected files. A dialog appears for each step, while the step is still running in the background. Read the dialog and always observe the last 5 selected files. Do not click the dialog buttons impatiently, or you will receive error dialogs and your file names will become disheveled. Be cautiously observant, Opus deselects files well before the new file names are listed. Wait until each rename step displays updated file names for the last 5 files before continuing.[/li][/ol][/li]
[li] The toolbar features both a Clean +1 Filenames button and a Clean +1 Filenames menu button. The menu button's menu contains the 10 individual steps (which are also displayed on the main toolbar itself as separate buttons).
[/li]
[li] The number of steps was reduced in v4.0 from 12 back to the original 10 by consolidating a couple of steps(v3.0 had 12 steps, v2.0 had 11 steps).
[/li]
[li] Safely operates on selected files that are already correctly named in the target file name format. If selected, these files are still changed throughout the multi-step process; but they just end up with the same file name they started with. This allows for a safer Select All rename operation (at least with the files included in my test cases). Considering the overall intent is to name files in a more-less standardized way here, this is possible. (This was added in v3.0, improved in v4.0, and preserved in v5.0).

The safety results were verified by making one rename pass of all steps, and then copying the file names into a text file. Then a second pass was made and the file names copied to a second text file. The two files were WinDiff-ed which correctly showed no changes between the rename passes.
[/li]
[li] Replaces any the following dot character placeholders (dot | hyphen | underscore | space) with an actual dot within version numbers. Thus "v1.2.3", "v1_2_3", "v1-2-3", and "v1 2 3" all end up as "v1.2.3".
[/li]
[li] Properly handles the several different dot character placeholders (dot | hyphen | underscore | space) on either side of the "v" or "V" that precedes the version number. This includes if one type of character is on one side and another type is on the other side. (Versions prior to v4.0 would incorrectly rename the file if the dot characters were not the same on both sides. V2.0 would do so if the preceding the "v" or "V" was not actually a dot.)
[/li]
[li] Renames files in Title Case.
[/li]
[li] Replaces several consecutive spaces or "space characters" (space | underscore | dot) with a single space (preserving any dots belonging to a version number or file extension).[/li][/ol]This thread is a great example of the need to have some means to force each Opus raw command in a button to wait until the previous command has completely finished.

Keywords: Race State example Rename button SYNC: wait pause button finished script RegEx Regular Expression
KRA-10StepsToFileNameBliss.dop (29.1 KB)
KRA-10StepsToFileNameBliss-TestCases-v5.0.zip (3.22 KB)

Regarding the monster solution proposed there, good grief ! Talk about pulling out all the stops...

I will run it through its paces now, though the holy grail of a one-shot routine is still tentatively beyond grasping.

As for the test cases, I tried to be as clear as possible about the "rules" for what I was after, but I'll try again:

1- Treat . and _ as a space, except when separating version numbers, where numbers should be separated by .
2- Pad all hyphens with spaces thus: # - #
3- Reduce multiple spaces to single spaces
4- Convert the filename to title case

Example would be:

SOME.FILE.V.1.2.3_TEMP.zip -> Some File V1.2.3 Temp.zip
another_file V_2_3.4.zip -> Another File V2.3.4.zip
HYPHEN-TASTIC -v4.rar -> Hyphen - Tastic - v4.rar

The capitalisation of the v's is unimportant really, though lowercase always looks better to me it's not a concern unless it's a trivial amendment.

I'm well aware that it's not a trivial problem, so if a perfect solution cannot be found then that's just life! :slight_smile: Two buttons is fine, that's the solution I'm currently working with (The one I posted) even if it does throw the odd error, it's not eaten anything yet!

Once you try Opus, you never go back... So true!

Re-download the toolbar. I had to fix the last step I forgot to remove the dot between the "v" and the version number. That's down now.

Re-Download it again. It's been renamed to the 11 steps! I also forgot about double spaces to single spaces.

Actually it does:[ul][li] Any number of consecutive spaces to a single space.[/li]
[li] Any number of consecutive underscores to a single space[/li]
[li] Any number of consecutive dots to a single space[/li]
[li] Any combination of the above to a single space[/li][/ul]One more step, and it's offically a 12-step program! :open_mouth:

1 Like

[quote="denyerec"]As for the test cases, I tried to be as clear as possible about the "rules" for what I was after, but I'll try again:

1- Treat . and _ as a space, except when separating version numbers, where numbers should be separated by .
2- Pad all hyphens with spaces thus: # - #
3- Reduce multiple spaces to single spaces
4- Convert the filename to title case

Example would be:

SOME.FILE.V.1.2.3_TEMP.zip -> Some File V1.2.3 Temp.zip
another_file V_2_3.4.zip -> Another File V2.3.4.zip
HYPHEN-TASTIC -v4.rar -> Hyphen - Tastic - v4.rar

The capitalisation of the v's is unimportant really, though lowercase always looks better to me it's not a concern unless it's a trivial amendment.[/quote]
This is why providing file examples up front help us help you. I wasn't aware you were also using underscores as well as dots between your version numbers. So the earlier versions did not provide this "v_1_2_3" would have ended up "v 1 2 3" not "v1.2.3". I have corrected this in the latest version of the download posted above. I have also ensured that the process can be safely run on files that are already correctly named (at least in my test cases). All versions will start with "v" (lower case).

You now need to download the 12StepsToFileNameBliss files.

I don't know if this button command code is good enough to do all you want, but you might give it a try. Note all command lines begin with the word sync:

sync: dopusrt /cmd Rename PATTERN "." TO " " FINDREP
sync: dopusrt /cmd Select RESELECT
sync: dopusrt /cmd Rename PATTERN "-" TO " - " FINDREP
sync: dopusrt /cmd Select RESELECT
sync: dopusrt /cmd Rename PATTERN "_" TO " " FINDREP
sync: dopusrt /cmd Select RESELECT
sync: dopusrt /cmd Rename PATTERN "(.) ([0-9]) ([0-9]) ([0-9])(.)" TO "\1\2.\3.\4\5" REGEXP
sync: dopusrt /cmd Select RESELECT
sync: dopusrt /cmd Rename PATTERN "(.) (.)#" TO "\1 \2" REGEXP
sync: dopusrt /cmd Select RESELECT
sync: dopusrt /cmd Rename CASE=allwords

[quote="JohnZeman"]I don't know if this button command code is good enough to do all you want, but you might give it a try. Note all command lines begin with the word sync:

sync: dopusrt /cmd Rename PATTERN "." TO " " FINDREP
sync: dopusrt /cmd Select RESELECT
sync: dopusrt /cmd Rename PATTERN "-" TO " - " FINDREP
sync: dopusrt /cmd Select RESELECT
sync: dopusrt /cmd Rename PATTERN "_" TO " " FINDREP
sync: dopusrt /cmd Select RESELECT
sync: dopusrt /cmd Rename PATTERN "(.) ([0-9]) ([0-9]) ([0-9])(.)" TO "\1\2.\3.\4\5" REGEXP
sync: dopusrt /cmd Select RESELECT
sync: dopusrt /cmd Rename PATTERN "(.) (.)#" TO "\1 \2" REGEXP
sync: dopusrt /cmd Select RESELECT
sync: dopusrt /cmd Rename CASE=allwords[/quote]

@John

I ran this against my test case files above, to see what would happen.
Oh boy did I see fireworks! Lots of dialogs started flying all over the place. CPU usage for dopus.exe also climbed rapidly to 100% and my system fan started ramping up because the CPU started to get hot! :open_mouth:

I'm assuming you meant for this code to work only on a single file.
But even when I tried this against a single test case from my list, I still incurred both the dialogs and the CPU usage.

By the way, the SYNC: keyword will not help here. That only tells dopus.exe to wait for an external command to complete before launching the next external command. However, dopusrt.exe reports itself as complete as soon as it hands off the command back to dopus.exe. Dopusrt.exe doesn't know if dopus.exe has completed the raw command or not. Thus, this technique just adds an unneeded step to the same results as just using raw commands internally without using dopusrt.exe. Leo informed me of this in another thread somewhere on the Centre here.

Well that's interesting Ken, I had no such problems on my end. The button commands fail for me without the sync: dopusrt code, but work just fine with it on the file name examples denyerec posted such as:

another_file V_2_3.4.zip
HYPHEN-TASTIC -v4.rar
SOME.FILE.V.1.2.3_____TEMP.zip

I did run my button on your file list and I noticed not all the files renamed correctly, but most did.

[quote="JohnZeman"]Well that's interesting Ken, I had no such problems on my end. The button commands fail for me without the sync: dopusrt code, but work just fine with it on the file name examples denyerec posted such as:

another_file V_2_3.4.zip
HYPHEN-TASTIC -v4.rar
SOME.FILE.V.1.2.3_____TEMP.zip

I did run my button on your file list and I noticed not all the files renamed correctly, but most did.[/quote]

Download my test cases .zip file, and try to rename them using your button.

I've just posted an improved version to my method above. See my behemoth post earlier in this thread.

For those who have downloaded the earlier version, I recommend downloading this one as well as the .zip file containing my test cases. Try out my method on the test case files before attempting it with your own files! The key is to wait patiently for each step to complete before launching the next one.

I still cannot get a single button to reliably, safely, and consistently perform all the required steps, in proper the sequence, without error dialogs (even on a single file). Thus the 10-button method. However, with my method you can do all the files in your folder.

I'm sure PC performance, processor speed, other processes running, file name length and number of character replacements affect the likelihood of the "Race State".

For the curious, all the extra numbers in my test cases are what I call "search and replace obstacles." Opus usually searches for numbers from the right to left. Since this often trips up people (or at least me) in regular expression, I include prefix, middle, and suffix numbers to search around in my test cases. This helps me insure my rename doesn't destroy numbers in the file names I want to keep.

My method above handles version wherever they occur and leaves other whole numbers alone wherever they occur. Decimal numbers that are not part of a version number (i.e. preceded with a "v" or "V" would be in trouble.

[quote="JohnZeman"]Well that's interesting Ken, I had no such problems on my end. The button commands fail for me without the sync: dopusrt code, but work just fine with it on the file name examples denyerec posted such as:

I did run my button on your file list and I noticed not all the files renamed correctly, but most did.[/quote]

I'm sorry John, I missed that bold sentence above before!

I just went through again and tried your button against my test case files. I might have rushed it before. It appears as if it is done (viewing the mouse hourglass) then another process kicks in. This time I was much more patient between each file and I indeed did not get any errors. So that is encouraging.

However...

I still observe dopus.exe utilize 97%-99% CPU. I just tested this on a second, pristine PC--it only has brand new Windows XP SP2 install, fully updated, with only its hardware drivers, and the latest Opus installed (plus a faster CPU). But, I'm still seeing dopus.exe pegged at 96%-99% CPU utilization. and this is 15 minutes after I clicked the button last, plus the time I've been typing this message. My PC is just sitting there with the meter red-lined.

Okay I've achieved measured success. I've updated my post above with the definitive solution. There is a button for one and only one selected file. And another for several selected files. Carefully read the updated post above and the dialogs while using the buttons. Both buttons do it all in one button. Neither will hurt files that have already been so renamed. Neither use 99% CPU time.

Try everything on my provided test cases first, before attempting with you own files. If you receive error dialogs, you most likely double-clicked the toolbar button, or clicked on a dialog button too early.

Please post feedback from your own tests.

Hi Ken,

I'm overwhelmed !
Fantastic !

I understand you're a bit worked up as you've obviously put much effort into this.
I'm still wondering how to type the '¬' character from the keyboard.

But Ken, I still stand by my original post.
I still think I may be able to do this 'My Way' .
I just need a few days.
If I fail so be it.
In any case congrats to you !

We're really not that far apart Ken.
I went to some of Elementary School in the Eastern part of the UP.
I attended Gros Cap west of St. Ignace.
At the time, we lived on US2 almost as west as Brevort, about 20 miles from St. Ignace.

My Boyhood beach is now an almost hidden State Pull Over on US2.
Much thanks from me to a Detroit Lawyer who had a summer home adjacent to us at the time.
Grandfather wanted State permission to build a staircase down to Lake Michigan.
He got it and built it.
Long gone, it has been replaced and protected by the State of Michigan.

:opusicon: Porcupine

[quote="porcupine"]I understand you're a bit worked up as you've obviously put much effort into this.
I'm still wondering how to type the '¬' character from the keyboard.[/quote]

Hi there Porc!
I'm rather proud of my Opus raw command technique of using {dlgstring} to pause overall execution. It's a wee bit of a hack, but it works! :laughing:

Yeah, you could say I've put some time into this one. Truth is, when Denyerec posted his question, I knew right away what he was asking for. I was trying to accomplish something very similar (as in nearly exact) a few months back. I tried a number techniques only to end in frustration. But I kept notes. When Denyerec's posted what he had tried, it gave me an idea today what I had been doing wrong in the past. It had to do with the general order of the replace and needing to use temporary placeholder characters. I also have learned about the "#" operator since a few months ago. This is what makes it hum.

The character "¬" (not sign) is my personal favorite, for the very reason you typed above--not many people know how to type it. This really makes it the best temporary character. The chance of it actually existing in a real file name is slight at best. It is actually part of both the ASCII and Unicode character sets. I use it in command script function variables for handling special characters that otherwise would choke a command script.

With the Numlock enabled, hold down the Alt key and type "0172" on your numeric keypad. It won't work from the regular number keys. Barring that, you can also copy and paste it from CharMap.exe.

Yeah we're basically at opposite ends of the same state. I haven't been to the U.P. since college--I deejayed a friend's wedding in Ishpeming. It's really beautiful up there.

I just completed a button prototype that undoes the first one! :smiling_imp:

Soon, we will have a file name formatting toolbar with at least two sets of buttons: one to reformat file names suitable for uploading to the Internet (removes spaces and caps and converts versions like "v1.2.3" to "v_1_2_3"), the other (posted already) takes Internet files and makes them the more palatable for human consumption in in Windows.

Quick somebody give me something else meaningful and useful to add to it!

I think if this job gets packed into 2 buttons (One for adding and one for stripping) the guy responsible should get an honorary "Dev" title! :wink:

It's been and continues to be an interesting learning experience. I've already, as a byproduct, managed to add several little shortcuts to Opus that are now in constant use. Loving it!

:smiling_imp: I'm still cooking here.

The next version is getting renamed and published in the Buttons and Toolbars forum.

I'm adding Support for "BeachesOfTheUSVirginIslands.doc" <-> "Beaches Of The US Virgin Islands.doc"

I'm also adding buttons for several steps that have value by themselves "V1.2.3" <-> "v1.2.3"

"&" <-> "+"

Any other ideas?