Button to move files based on first letter?

Is it possible to make a button that will look at the first letter of a selected file, and then if it starts with the letter "A," move it to the folder "D:\Folders\A," or if it starts with "B" to "D:\Folders\B," and so on for all the letters of the alphabet? Thanks.

Rename REGEXP PATTERN="(.)(.*)" TO "\1\\1\2"

<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>&amp;Move into New Alphabet Folder</label>
	<tip>Move the selected items into a new alphabetical folder</tip>
	<icon1>#move</icon1>
	<function type="normal">
		<instruction>Rename REGEXP PATTERN=&quot;(.)(.*)&quot; TO &quot;\1\\\1\2&quot;</instruction>
	</function>
</button>

Well well... - dynamic folder creation using Rename. Very neat. Something else I either never knew about DO or have long since forgotten. Thanks for the example.

Regards, AB

Thanks. Will this still work if the folder already exists? (Forgive me if that's a dumb question. I don't understand regex at ALL.)

In fact, now that I think of it, wouldn't this button move the selected file into an alphabetically named subfolder in the same folder as the selected file? I probably wasn't particularly clear earlier, but I'm hoping to be able to select a file whose name begins with "A" from any folder anywhere and move it to "D:\Folders\A," and so on.

Correct.

This code should work for you:

Rename REGEXP PATTERN="(.)(.*)" TO "D:\Folders\\\1\\\1\2"

Yes. If the folder is already there, it will just move the file(s) into that folder. If the folder is not there, it will create the folder first and then move the file(s) into it.

Be aware that rename won't work across drives.

(Edit: It does work these days.)

Indeed, very neat but I can't take any credit for it... that goes to Leo as usual :smiley:

This is a great, simple script. Thank you :slight_smile:

With some help from Leo, I've modified the script as follows:

@filesonly
Rename REGEXP PATTERN="(.)(.*)" TO "\1\\\1\2" AUTORENAME

This is the same script, except it ignores folders (otherwise you get an error if you have selected one of the target folders by accident, as it tries to move it into itself. It also autoincrements the name if there's a clash.

Is it possible to fix this so it moves all files starting in something else than a-z into a single folder called “#”?

A bit too much for a RegEx, but a rename script can easily handle this.

function OnGetNewName(getNewNameData) {
    var tmp = getNewNameData.newname;
    var firstLetter = tmp[0];

    if (/[a-z]/.test(firstLetter.toLowerCase())) {
        var newFolder = firstLetter;
    } else {
        var newFolder = '#';
    }

    return newFolder + '\\' + tmp;
}

17132.orp (557 Bytes)

3 Likes

This is what I've done to achieve something similar

@filesonly
Rename REGEXP PATTERN="^([a-z0-9])(.*)" TO "\1\\\1\2" AUTORENAME
Rename REGEXP PATTERN="^([^a-z0-9])(.*)" TO "__MISC\\\1\2" AUTORENAME
2 Likes