Moving Selected Files into Folders Minus Last 4 Characters

Hello Dopus/Coding Experts,

Can anyone help me with this much needed code please?

I currently use:

@nofilenamequoting
@set dir={dlgstringS|Enter name of folder to move selection into|{file|noterm|noext}}
Copy MOVE TO="{sourcepath$}" CREATEFOLDER="{$dir}"

This pops up a dialog with the first selected file name inserted. I delete the last 4 characters and click OK. This creates the folder and moves all selected files into it.

What I would like is for no dialog to open and for it to just remove the last 4 characters on it's own, then move the files as before. It would be nice if it ignored case sensitivity for moving all files and would work when a bunch of mixed names are selected.

Example filenames that would be selected:

Apple-Tree-Cat-Dog-1-001.jpg
apple-tree-cat-dog-1-050.jpg
Apple-Tree-cat-Dog-14-097.jpg
Apple-tree-Cat-dog-14-342.jpg
apple-Tree-swimming-Pool-08-094.jpg
apple-Tree-Swimming-pool-08-689.jpg

From the names, 3 folders would be created:

/Apple-Tree-Cat-Dog-1/
/Apple-Tree-cat-Dog-14/
/apple-Tree-swimming-Pool-08/

All the files with the same base name (full name minus the Extension and last 4 digits) would be moved into their respective folders.

I was able to create a Regex that selects the last 4 digits (-[0-2]\d{2}) but now I'm lost. I can't figure out how to mark all the rest of the name as group1 nor would I be able to figure out how to make it all work in a button. Or if this method is even the correct direction to go.

Thanks for any help!

image
I'm not expert, just try to help you with regular expression
/^([^.]+)(-[0-2]\d{2})/
Here you can see the 1st group match as your expectation.
or If you want to capture the file extension in a group then you can try this one

/^([^.]+)(-[0-2]\d{2})(\..*)/
1 Like

The Advanced Renamer seems to be the more suitable tool.

Parameters for the Advanced Renamer
Mode: Regular Expressions
Old name: (.*)(....)
New name: \1\\\0
Ignore extension: checked

44532.orp (194 Bytes)

2 Likes

Thanks, Khalidhosain for the help on getting the rest of the filename!

Thank you so much!, lxp. Works great. I was just playing around with Advanced Renamer hoping I could figure it out that way. I could not.

Can I ask? How does (....) know to remove 4 away from the end and and to go backwards?

And whats \0? I'm guessing it means the original name.

I'm so lost with Regex and any kind of programming :slight_smile:

.... is four random characters at the end of the string. .* is zero or more characters. This part just takes what's left to make the whole expression fit.

Correct :slight_smile:

1 Like

Ok, I get it now. It's like .... is being read first and blocking it from being read again. Then .* is starting from the beginning (since it's entered in first) and filling in the rest until stopped by whats already been read. At least thats how I visualize it in my head :+1:

Thanks again for the help, good sir!