Question: Move files to subfolder using Reg Exp

Advanced Renamer forum
#1 : 15/12-24 23:18
Jamie
Posts: 3
Hi

I have been googling this forum and I found the script mode to move files to subfolders that had been named with first "n" characters in the filename.

item.newPath = item.path + item.newName.slice(0,10); <-- for example.

Now, I saw Move mode to use substrate too.

These work fine with files with longer than 10 characters in their names.
However, if the file contains less than 10 characters it fails to create/move subfolders.

What I am trying to achieve is... for example

i.e file names below

advanced renamer 23.epub
advanced renamer 2.epub
bell 4.epub
bell 234.epub
bell 12834.epub
advanced renamer 231.epub
-----------------[ these files always end with one to three digits numbers]

Move these files to each subfolers naming "bell" and "advanced renamer (or shorter)"

I think if I use regular expression of (^[^0-9]{0,10}).* would work to "extract or catch" first 10 letters.

Is there anyway to do such task using the script function or other mode in Advanced Renamer?

Thank you for you help in advance.

edited: 15/12-24 23:20
#2 : 16/12-24 01:50
Jamie
Posts: 3
Reply to #1:

I think I solved my own question....

Even though I couldn't get the names sliced to 10 characters, I was able to get files into subfolders with the file names without numbering with following:

item.newPath = item.path + item.name.match(/^.*(?=\s\d+.*$)/) + "\\";

If anyone can now help me to shorten the names, I would greatly appreciate.

edited: 16/12-24 01:51
#3 : 16/12-24 01:58
Delta Foxtrot
Posts: 406
Reply to #2:

Hi Jamie, welcome,

EDIT and spoiler alert: See the next post for my final answer. END EDIT

You're on the right track. If the target folder name is always followed by a space, then a number, this will do it:

test = item.name.match( /^[^\d]+/ ) ;
item.newPath = item.path +
( test[0].substr( 0, test[0].length - 1 ) ) ;

The script just extracts the portion before the number to a variable, then chops off the space at the end, and makes the remainder the new folder name.

EDIT: second line could have been "item.newPath += test[0].substr(0, test[0].length - 1) ;"

Not sure if you'll find this interesting, but another way to do it:

pattern = /[a-z]+/i
for ( j = 1; j < 20; j++ ) {
testStr = app.parseTags("<word:"+j+">") ;
if ( pattern.test( testStr ) ) {
item.newPath += ( j > 1 ? " " + testStr : testStr ) ;
} else {
break ;
}
}

This one just takes a "word" of the filename at a time. If it's not a number it adds it to the new path until it encounters a number; then it breaks out of the loop and returns. It's longer but I like it. If I had a thousand files to move it might or might not be slower. :)
END EDIT

Be sure to use RENAME or COPY modes. If you use Copy mode you'll get a copy in the new location. Rename mode moves the file.


Best,
DF

edited: 16/12-24 03:40
#4 : 16/12-24 03:38
Delta Foxtrot
Posts: 406
Reply to #3:

FINAL ANSWER!

item.newPath += item.name.substr( 0, item.name.lastIndexOf( " " ) ) ;

: )
#5 : 16/12-24 14:36
Jamie
Posts: 3
Reply to #4:
This worked perfectly. Thank you!