Copy different files to the equivalent folders

Advanced Renamer forum
#1 : 04/12-24 11:36
John Ninos
Posts: 2
Hey guys, I'm trying to find a solution to the following but I don't know if this is possible with Advanced renamer, so any help would be much appreciated, thank you!

I have several folders which inside have a .png file (all the other files are different extensions if this helps) which name is several random letters and "_Preview" (ex. sdkjsajd_Preview.png in another folder the equivalent file would be djasdajd_Preview.png etc.)

For these files as I know that "_Preview" is the common word I windows search and can drag all these files to advrenamer. Now what I would like to do is to copy or move these files to another folder path which subfolders have the same random letters (ex. Asset_sdkjsajd_file in another folder the equivalent file would be Asset_djasdajd_file0123).

As this can be tricky here is a diagram that could help:

"ajshdjah_Preview.png" - > to be automatically inside a folder that has "ajshdjah" in the folder name but this needs to be automated not to put path for every file because I have tons of folders


I hope that makes sense in any way, once again thank you in advance :)
#2 : 04/12-24 15:18
Miguel
Posts: 163
Reply to #1:
Hi John.
I hope I understood what you want.
If you want to copy or move files to folders with the random letters of the files in the folder´s name you can use :
<Substr:1:_> as the name folder.
For example:
Once you select Move or Copy in the addres bar write:
Output Folder: x:\Destination folder\<Substr:1:_>

If you want to rename the files at the same time can add a New Name method:
New Name: Asset_<Substr:1:_>_File

I would use the copy method so I can avoid any possible problems. Anyway, you can always use Undo Batch.


Miguel



edited: 04/12-24 15:21
#3 : 04/12-24 16:58
John Ninos
Posts: 2
Reply to #2:
Hi Miguel,

thank you for the reply, I don't want each png file to create a folder I want rather to move the png files to the equivalent folders based on a name:

I have a folder which inside has png images named example as follows:

sdsbo_Preview,
sefw3_Preview,
sepsb_Preview etc.

in the same folder, I have folders that have names like:

Aset_structure_cement_M_sdsbo_LOD0,
Aset_structure_cement_M_sefw3_LOD0,
Aset_structure_cement_M_sepsb_LOD0 etc.

I want somehow the png files to detect the common pattern naming and move or copy to the equivalent files based on the common "code" on their name

thank you much! I hope it's a bit more clear now!
#4 : 04/12-24 17:51
Delta Foxtrot
Posts: 364
Reply to #3:

Ya Sas John, welcome,

Your second post was MUCH more clear. It's always a good idea to include real examples of filenames (before and after) and directory names/paths when talking about move/copy operations.

Advanced Renamer has no native awareness of directories (other than the ones in the path of the file/s), so you'll have to supply that information in the form of a directory list (generated for example in Windows command prompt). I can make a file that will use that information to search the list of folders for matches to the first part of the filename. I'm a little busy this morning but I should be able to knock it out this evening.

Questions:
1. Are you familiar with Windows console and DOS-style commands?

2. I'll need the target path of the files and folders in question (like for instance "j:\projects\specificatons\" (Not really a question, but...) :)

EDIT: Here's a thread with approximately the same problem. In it I explained what has to happen in order for this type of transformation to work correctly. You might want to check it out in the meantime: https://www.advancedrenamer.com/forum_thread?for um_id=15188
END EDIT

Best,
DF

edited: 04/12-24 18:24
#5 : 04/12-24 20:13
Delta Foxtrot
Posts: 364
Reply to #4:

Hello again John,

I got to it sooner than I thought. Just need that information and we *should* be set.

Screenshot: https://drive.google.com/file/d/1EOKi0gvttmMEbGb 13jrC7d0BMBIFInLO/view?usp=sharing

EDIT: I have to go out so I'm just going to post the script. You'll need to create a .csv file using the instructions in that other thread I listed above each time you need to use the script. And change the first two non-comment lines in the PRE-BATCH SCRIPT to match your directory path (**DOUBLE-BACKSLASH enclosing each subdirectory!**) and (if you change the .csv filename from "test.csv") the .csv filename. Just put the two parts of the script in the appropriate place (I marked the "Pre-batch script" button with an arrow on the left in the screenshot).

//------------------------ PRE-BATCH SCRIPT:

// The following line holds the target directory - LEAVE THE QUOTES and USE DOUBLE
// BACKLASHES TO ENCLOSE EACH SUBDIRECTORY!:
const CurDir = "J:\\forum work\\JohnNinos\\" ;

// And this next line is the csv filename:
var csv = CurDir + "test.csv";

csv = '::\"' + csv + '\"';
const folderList = [];
j=1;
while (csvLine = app.parseTags('<File Line:' + j + csv + '>')) {
folderList[j-1] = csvLine ;
j++;
}
//------------------------


//------------------------ MAIN SCRIPT:
fileExt = item.ext.toUpperCase() ;
if ( fileExt !== ".PNG" ) {
return ;
}
const toMatch = app.parseTags('<word:1>') ;
for ( k = 0 ; k < folderList.length ; k++) {
matchStr = folderList[k] ;
if ( matchStr.search( toMatch ) !== -1 ) {
item.newPath = CurDir + folderList[k] ;
return ;
}
}

//------------------------

If this needs tweaking let me know (also if it works right :). Be sure and save the batch, and remember to preview the new path. If something goes wrong you can use the "Undo" feature in ARen to get back to square one. Be sure you are using the latest version of ARen.

After posting the update I realized I wanted to make it work only with .png files. I added that part. You can load all the files in the directory and it will only look at the .png files.

END EDIT

Best,
DF

edited: 04/12-24 21:41
#6 : 06/12-24 19:19
Miguel
Posts: 163
Reply to #5:
Your script works like a charm. I know you designed it based on John's question. Dude, you empower Advanced Renamer with your scripts. We're lucky to have you on the forum.
I've been testing it for a while and noticed that it only works if the common word is at the beginning of the filenames.
const toMatch = app.parseTags('<word:1>') ;
For example:
FileName abcdef123 more word.png. This name is not processed
Is there a way to fix this so that the position of the common word doesn't matter?
Also, can you add more extensions to the file type to process?
I know if I remove that part all files are processed, but suppose I want to process png and jpg for example.

Thanks again for your amazing work.
Hugs.
Long live Texas!!

Miguel
#7 : 06/12-24 21:08
Delta Foxtrot
Posts: 364
Reply to #6:

Hey Miguel,

Thanks for your kind words my friend.

You could change that const toMatch... line to anything that will match. Just changing <word:1> to <word:2> should fix the problem for that specific filename you mentioned. But you could also use other ways to define the word, like a substr(), substring(), or slice() for instance. Or you could use split(" ") if all the words in your filename are separated by spaces, then you'd have an array of the words in your filename; -OR- if your keyword always contains some specific phrase you could use a match() statement with a regexp. With those methods you should be able to use whatever you wanted. Let me know if you have a specific use case you want me to address.

To add more file types, I would change this block:
//-----------
fileExt = item.ext.toUpperCase() ;
if ( fileExt !== ".PNG" ) {
return ;
}
//----------
to:
//----------
fileExt = item.ext.toUpperCase() ;
if ( fileExt == ".PNG" || fileExt == ".JPG" ) {
} else {
return;
}
//---------
A switch/case statment would be "cleaner" but not really necessary, since the "if" works fine. You just have to change the "!==" (not equal) to "==" (equals) and divide the parts by "||" (logical OR). EDIT: and of course make the statement just "fall through" on a match, returning if the "if" fails. END EDIT

Long live Canaria! :)
DF

edited: 06/12-24 21:10
#8 : 07/12-24 15:28
Miguel
Posts: 163
Reply to #7:
Hi DF,
Thanks for your reply.
First of all I want thanks you for your patiente.
I don´t have any specific case, simply I like explore all the posibilities.

I have made the changes for add more file types and works perfectly.

I remembered one of your post where you said that programers use the symbol "|" or "pipeline" to separate commands.

This morning I have used the symbol and it works perfectly

-------------------
const toMatch = app.parseTags('<word:1>|<word:2>|<word:3>') ;

https://drive.google.com/file/d/1w-i7YBlbVg3QQGm YxbKgbIX-Yd5urytN/view?usp=drive_link
------------------------
BUT!!! There are some inconsistencies. If I add, for example, <word:4> and there are not any file that macht then the others files are move to wrong folders. In my example all files go to Italy folder
Perhaps you could correct it with a professional touch .

Again,
Thanks for all.

Miguel

PS. Did you knew that Canary Islands and Texas had historic conexion in the past? Canarian families founded "San Antonio" in Texas and even the texas longhorn had their origin in the canary cow. :)))
#9 : 07/12-24 17:17
Delta Foxtrot
Posts: 364
Reply to #8:

Hey Miguel,

Now that you mention it I seem to recall learning that historical fact about the settlers of S.A. I went to school long enough ago that we actually learned some history. As to the longhorn, I don't think any Texan will ever admit that our beloved longhorns came from anywhere except maybe just springing up out of the ground fully-formed! :)

The single-pipe "|" is what is called a "bitwise" OR. It only compares ones and zeros in a binary number and will yield unpredictable results otherwise. You can read about it here if you want a headache: https://www.w3schools.com/js/js_bitwise.asp`

But that's not what's happening in your app.parseTags() statement. What you've done is make a string of the first three words in your filename, each word separated by a "|" character. If you were to turn it into a bitwise OR statement ("const toMatch = app.parseTags('<word:1>'|'<word:2>'|'<word:3>') ;" with single-quotes around each word tag) you'd get a very different result, probably a parse error.

As Kim says (in the javascript documentation, I think), only use a phrase in the app.parseTags() command that you would use in a NEW NAME method: tags and printable characters mainly.

I'm still not sure exactly what your intention is here. Do you want to have the script compare filenames word-by-word with folder names? Please explain and I'll try to figure what needs to happen.

Sorry for being so dense; it's a Texas thing I guess! :)

EDIT: Oh, by the way you have two semicolons on the return statement in you extension parsing block; all that does is make a zero-length javascript statement, so it doesn't error, but I thought I'd mention it... <shrug>

Best,
DF

edited: 07/12-24 17:20
#10 : 07/12-24 18:51
Miguel
Posts: 163
Reply to #9:
Hi,
I'm sorry I didn't explain myself well.
My programming knowledge is zero.
I only want not to have to edit that line every time I use the script.
Example:
abcd is the common word.png
Now abcd is in second position.png
Now is abcd in third position.png
and so on.
The script will work regardless of the position of the common word.

Miguel

Let´s finish this because John is not going to find the solution to his problem.

#11 : 07/12-24 22:31
Delta Foxtrot
Posts: 364
Reply to #10:
Darn, Miguel, I didn't follow my own rule! : ) I'm starting a new thread for this, see ya there...