Moving Group of Files to a Folder

Advanced Renamer forum
#1 : 20/10-21 00:01
Tho Con
Posts: 6
Hi,
Can someone please help me.
I want to move a group of files to a folder and keep same file name.
Basically use the name of file before "_ddmmyyyy" to create a folder for that file and move all same group file to that folder.

example:
file1_ddmmyyy xxxxxxxx.jpg (where is ddmmyyyy is random day, date, year)
file1_ddmmyyy xxxxxxxx.jpg (where is ddmmyyyy is random day, date, year)
file2_ddmmyyy xxxxxxxx.jpg (where is ddmmyyyy is random day, date, year)
file2_ddmmyyy xxxxxxxx.jpg (where is ddmmyyyy is random day, date, year)

move all files above to file1 and file2 folders.

Thank you


#2 : 20/10-21 00:36
David Lee
Posts: 1125
Use Batch mode: Move
Output folder: <Substr:1:"_">

Leave the Method List blank unless you want to rename the files at the same time as moving.
#3 : 20/10-21 19:57
Tho Con
Posts: 6
Reply to #2:
Thank you for your help,
it doesn't working well for some files like:

file with double underline
file__ddmmyyyy_xxxx_xxxxxxxxxxxxxxx

or underline at front
_file_ddmmyyyy_xxxx_xxxxxxxxxx

but all files will have "_ddmmyyyy" after it.

example of "_ddmmyyyy" can be: _19102021

Thank you
#4 : 21/10-21 09:46
David Lee
Posts: 1125
Reply to #3:
Use Batch mode Rename with a Script method:

folder = item.name.match(/^_?([^_]*)/)[1];
item.newPath = item.path + folder;
#5 : 21/10-21 16:50
Tho Con
Posts: 6
Reply to #4:
Hi David,
Thank you for taking your time to help me.
your script was working but the result not as I expected.

can you make a script that can make a folder with a name of file before _ddmmyyyy and move all those group of files to that folder?

example with files:

_hello_12122021_1820_testing
_hello_15072021_1213_WORD
hello__12092021_2720_FOR TESTING
hello__19042021_2230_testing
hello_car_12102021_3085_ WHITE
hello_car_25122021_6454_RED

So there will be 3 group of files name and create a folder by those name:
_hello
hello_
hello_car

Thank you


edited: 21/10-21 17:18
#6 : 21/10-21 19:49
David Lee
Posts: 1125
Reply to #5:
Does this do what you want?

folder = item.name.match(/^.*?(?=_+\d{8})/);
item.newPath = item.path + folder;
#7 : 21/10-21 20:14
Tho Con
Posts: 6
Reply to #6:
Hi David,
Thank you for fast reply.
your code working much better, but still not exactly what I need.

it will not work for files like this:

hello__12092021_2720_FOR TESTING (2 underscore between "o and 1")
mycar____27052021_3515 YELLOW (4 underscore between "r and 2")

the code only create folders without any underscore:

hello
mycar

instead of:

hello_ (1 underscore after "o")
mycar___ (3 underscore after "r")

Thank you for your help.






#8 : 21/10-21 21:00
David Lee
Posts: 1125
Reply to #7:
folder = item.name.match(/^.*?(?=_\d{8})/);
item.newPath = item.path + folder;
#9 : 21/10-21 21:09
Tho Con
Posts: 6
Reply to #8:
Hi David,

You are AMAZING. I don't understand the code, but it work 100%

I just purchased full version.

Thank you very much for your super help. :)
#10 : 21/10-21 23:35
David Lee
Posts: 1125
Reply to #9:
Actually the following, slightly simpler, line of code also works:
folder = item.name.match(/^.*(?=_\d{8})/);

"item.name" simply returns the original filename as a string and the "match" method will try to match the string that is defined in the argument of the method.

This could be a simple quoted string in which case, for example: "filename".match("lena") would return the string "lena".

However, in our case, we are using a Regular Expression as the argument, which is defined by enclosing it in "/" characters rather than quotes.

So to explain the regex /^.*(?=_\d{8})/ :
Regex uses "metacharacters" ie characters that have special meanings for example "." will match any character (incidentally if we really want a full stop to have its normal meaning in the regex we have to "escape" the character using "\" - ie as "\.")

"*" indicates any number of repetitions of the preceding character (including zero) and "^" signifies the start of the string. Thus "^.*" will match any string starting from the beginning of the filename, which on its own would simply return the entire filename.

However the construction (?=str) - known as a "positive lookahead" - specifies that the preceding match must be followed by the pattern defined by "str" - which in this case is "_\d{8}"

Here the metacharacter \d specifies a decimal digit and {8} means that the preceding character must be repeated a total of 8 times.

Thus the match will return all the characters from the beginning of the filename, stopping just before it encounters an underscore followed by the 8-digit date string.



edited: 21/10-21 23:38
#11 : 22/10-21 01:33
Tho Con
Posts: 6
Reply to #10:
Hi David,
Thank you for taking time to explain it to me.
you are genius :)