Hello,
I currently have a script expression which checks a directory for a folder with the name beginning with the name of the file passed through it. I use this in the Set Hierarchy element and it will put the matching folder into the hierarchy. For example, if I have a file named "ABC123.tif" would be put into a folder named "ABC123 ANYTHING". While handy, this is not what I am trying to do in this particular flow.
Here is the script:
var folder = new Dir("/Volumes/intheworks/Jobs/Enfocus Switch/Hierarchy Test");
var dirList = folder.entryList("*",Dir.Dirs,Dir.Name);
var destinationFolder = "";
for (var i=0; i<dirList.length; i++) {
if (dirList[i].startsWith(job.getNameProper()) == true) {
destinationFolder = dirList[i];
}
}
destinationFolder;
I am trying to modify the script so only the first 2 letters of the file name need to be at the beginning of the folder name added to the hierarchy. So a file with the name "ABC123.tif" would be moved to a folder named "AB ANYTHING". I think I can do this with regular expression but am unsure how to add this to javascript. I'd greatly appreciate any help.
Thanks bens!
The 0,2 worked perfect. Also, thanks for reg ex advice.
I have one more question about this.
What if the folder name had the 2 letters I wanted to match somewhere else in the folder name? I was just thinking, this could help us in another workflow so what if the file name "ABC123.tif" should be moved to a folder named "654321_AB_ANYTHING"? Would using the substring be an option for the directory list?
var folder = new Dir("/Volumes/intheworks/Jobs/Enfocus Switch/Hierarchy Test");
var dirList = folder.entryList("*",Dir.Dirs,Dir.Name);
var destinationFolder = "";
var key, reg;
for (var i=0; i<dirList.length; i++) {
key = job.getNameProper().substring(0,2);
reg = new RegExp ( ".+_"+key+"_.+" , "" ); //Given that you are looking for a xxx_KEY_xxxx pattern in the folder name;
if ( reg.test ( dirList[i] ) ) {
destinationFolder = dirList[i];
break;
}
}
destinationFolder;
Thanks Loic,
I can't get this to work.
I understand the concept and what you've posted seems to make sense, so I don't understand why it doesn't work. To my untrained eye, everything looks correct. Does this work when you run it?
var folder = new Dir("/Volumes/intheworks/Jobs/Enfocus Switch/Hierarchy Test");
var dirList = folder.entryList("*",Dir.Dirs,Dir.Name);
var destinationFolder = "";
var key = job.getNameProper().substring(0,2), reg, fo;
var n = dirList.length;
for (var i=0; i<n; i++) {
fo = dirList[i];
reg = new RegExp ( ".+_"+key+"_.+" , "" ); //Given that you are looking for a xxx_KEY_xxxx pattern in the folder name;
if ( reg.test ( fo.name ) ) {
destinationFolder = fo;
break;
}
}
destinationFolder;
Hey Loic,
This is still not working. The error Switch logs is "Error in line 13 of script Path segment 1: Error. Trying to access undefined member 'name"
The first script you posted through this error: "Error in line 13 of script Path segment 1: TypeError. 'test' undefined or not a function"
To test, I'm using a folder named "654321_AB_ANYTHING" and a file named "ABC123.tif"
var folder = new Dir("/Users/sunsikim/Desktop/test");
var dirList = folder.entryList("*",Dir.Dirs,Dir.Name);
var destinationFolder = "";
//Getting the two first letters as a key
var key = job.getNameProper().substring(0,2), reg, fo;
//retrieving dirs count
var n = dirList.length;
//Loopîng through the dirs
for (var i=0; i<n; i++) {
//Referencing the nth folder
//dirList[i] returns only dir name
//so you need to instantiate a valid dir object
fo = new Dir ( folder.path+"/"+dirList[i] );
//Declaring the regular expression object
//We use teh constructor as we need to build the expression with a dynamic item (i.e. the key).
reg = new RegExp ( "^.+_"+key+"_.+$" ); //Given that you are looking for a xxx_KEY_xxxx pattern in the folder name;
//Checking if teh folder name contains the key
var found = fo.name.match(reg);
//In case of…
if ( found ) {
destinationFolder = fo;
break;
}
}
//Logging whatever folder was identified
s.log(1,">"+ destinationFolder.name );
//Retrieving teh folder object
destinationFolder;
dir.entryList gives a list of strings, so you don't need the .name
RegExp does not contain a test() function, you can use search() instead. search() returns -1 if the regular expression doesn't match, and a nonnegative number if it does.
Note that in this script "fo" is only the name of the folder, not the full path. If you want the full path you will need to combine it with "folder.path", e.g.