Move file that cointains x words to a side folder that contains X words

Advanced Renamer forum
#1 : 08/11-24 08:49
Luis
Posts: 2
E.g.
If file name contains 'stranger_starwars' move to Folder that cointains 'stranger_starwars'.
If file name contains '2ndAlbum_chapter_Rock' move to a Folder that contains 2ndAlbum.

In other words:

Move the file(s) to the folder that equals the first 3 words on the file.

E.g.

If a file contains:
1897568G_Tuareg_CamOrig

Move it to a (side) folder within the same path that has in its name:
1897568G_Tuareg_CamOrig. (such as in "1897568G_Tuareg_CamOrig_Barrocoart_4K_C3")

Or
"1897568G and CamOrig" (as in "1897568G_Tuareg_CamOrig_Barrocoart_4K_C3")

I am using a Mac (also have a PC if needed).

Thank you!

edited: 08/11-24 08:55
#2 : 08/11-24 14:13
Delta Foxtrot
Posts: 323
Reply to #1:

Hi Luis,

I'm a little confused by your post, but here's how you can do what you specifically asked - move files to folders named for the first 3 words of the file.

Set "Batch mode:" to move or copy, then set "Output folder:" to
<word:1> <word:2> <word:3>
-or-
<word:1>_<word:2>_<word:3>

...or whatever format you want. You may have to fiddle with the setting for word delimiters, but that's all it should take (see Options/Settings/Renaming tab, field "Word separators:")

Screenshot: https://drive.google.com/file/d/1BkUpnIYQ90CZyAb PnuPo5BktaHBsu9Ax/view?usp=sharing
(I put one method in the method list so I can save this batch operation if needed. It just makes sure that any space in the filename is not multiple spaces; I put it in most of my batch ops.)

Best,
DF
#3 : 08/11-24 17:59
Luis
Posts: 2
Reply to #2:

Hi Delta, thanks!

I tried this before without success, also I tried the separators "_" as you are suggesting below but it creates a new folder with the first 3 words (and moves it there) instead of moving the files to the folders (already in existence) cointaining those 3 words.

So those folders contain other words as well beyond the 3 first words of the files.

Here are my screenshots:
Screenshot 1:
https://drive.google.com/file/d/1A28zqCcTrhLvnPZ 6eaoSvZMRATTq1LIk/view?usp=sharing

Screenshot 2:
https://drive.google.com/file/d/1O65JlwKoOi7cudk TaFUEuF1aK3XLUdN3/view?usp=sharing

So I want files which cointain "1897568G_Tuareg_CamOrig" (the first 3 words) as in:

1897568G_Tuareg_CamOrig_H.265-Apple4K
1897568G_Tuareg_CamOrig_C5_2of3
1897568G_Tuareg_CamOrig_C4_2of3

To move to (an existing) side folder (of the same path) 'containging' or matching the first 3 words ("1897568G_Tuareg_CamOrig") such as in the case of:

E.g.
"1897568G_Tuareg_CamOrig_The Cart and the Horse didn't get along_HWD"

In other words:
How to move files to existing folders (with same parent path) with a different name but containing the first 3 words of the original file?

Regards,
Luis




edited: 08/11-24 20:28
#4 : 08/11-24 23:43
Delta Foxtrot
Posts: 323
Reply to #3:

Hi Luis,

Ok, I think I understand what you are looking for (but I'm not positive). I'm pretty sure there's no way to do it using non-script methods, but maybe someone else on here can come up with a way. In the meantime, I wrote a script that I *THINK* does what you want. [Props to David Lee, script ninja, for a lot of it] It's not strictly plug-and-play, but it's not difficult to use either.

Caveats: You can only use one directory of files at a time. The sub-folders to move to must be in the same directory as the files, and both the files and sub-folders must start with exactly the same three "words" separated by the same delimiters (in the script I used an underscore like your example, but that can be easily changed). Finally, I'm not sure if it will work on a Mac, but I don't see why it wouldn't. And just remember, if something goes wrong you can always undo the last batch if you don't change anything before the undo procedure.

For the sake of explanation I'm using an example where you have your files and sub-folders in a directory named "C:\luis\files".

All right, here's the drill:
1. You have to create a csv file (in this script it's called test.csv, but that's easily changeable). On windows you'd open a console window, navigate to your target directory ( cd "\luis\files" ), then enter "dir /AD /b > test.csv" (no quotes). That will create a csv file with all your sub-folders listed, one to a line.
2. Start an ARen instance and create a new SCRIPT method. Press the "Pre-batch script" button and copy the following into that window:
//------------------------
// The following line holds the target directory:
const CurDir = "C:\\luis\\files\\" ;
// And this next line is the csv filename:
var csv = CurDir + "test.csv";

const NumDirs = app.parseTags("<Num Dirs>") * 1 ;
csv = '::\"' +csv + '\"';
var List = [];
j=1;
while (Line = app.parseTags('<File Line:' + j + csv + '>')) {
Match = Line.match(/(.*)/);
List[j-1] = Match[0] ;
j++;
}
//------------------------

Change the line that starts "const CurDir = " to match your actual directory; just be sure to leave the \\ marks in place and, if you add more subdirectories in the path separate them with the same "\\" dividers. If you want to use a different csv filename you can change it in the line after that one.
Press the "Save and apply script" button at the bottom of the window, then copy and paste the following into the main script window:
//------------------------
const toMatch = app.parseTags("<word:1>_<word:2>_<word:3>") ;
for ( k = 0; k < NumDirs; k++) {
matchStr = List[k].substr( 0, toMatch.length ) ;
if ( matchStr === toMatch ) {
item.newPath = CurDir + List[k] ;
return ;
}
}
//------------------------
(The first line contains the template for your filenames; if you need different delimiters you would change them there)

Click "Apply script" and save your batch as an .aren file so you can reuse it. (if you want to)

3. Drag your files in. Check the preview window (the "New Path" column) to make sure your files are headed where you want them. If so, execute the batch.

Like I said at the start, I'm still not sure this is what you want, but if not we may be able to adjust it. Let me know!

EDIT: I had to change the main script; I had the counter variable as "i" instead of "k", but inside square brackets "i" is the sign in this forum to turn on italics (and the brackets and the i disappear from the post). That ALWAYS bites me! :) I didn't test if after the change but it should be fine, I don't think I missed any needed changes (or the post would still partly be in italics. OK, I went back and tested it. Worked as expected. END EDIT

EDIT 2: If you set the batch mode to "Copy" and leave the "Output folder:" field empty this script will copy the files into the directories instead of moving them. That can be useful in testing. Thanks to Miguel for that one! END EDIT

Good luck,
DF

edited: 09/11-24 00:07