Modify Script - Move File to Folder....

Post Reply
Danny
Member
Posts: 27
Joined: Wed Dec 08, 2010 5:59 pm

Modify Script - Move File to Folder....

Post by Danny »

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:

Code: Select all

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.

Thank you in advance.

Danny
bens
Member
Posts: 130
Joined: Thu Mar 03, 2011 10:13 am

Re: Modify Script - Move File to Folder....

Post by bens »

Try this:

Code: Select all

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().substring(0,2))) {
    destinationFolder = dirList[i];
    break;
  }
}
destinationFolder;
I'm not exactly sure whether substring includes the last character, so you may need to use substring(0,1) instead.

If possible, stay away from regular expressions unless you like having headaches ;-)
Danny
Member
Posts: 27
Joined: Wed Dec 08, 2010 5:59 pm

Re: Modify Script - Move File to Folder....

Post by Danny »

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?

Thanks again for your help!
loicaigon
Member
Posts: 147
Joined: Wed Jul 10, 2013 10:22 am

Re: Modify Script - Move File to Folder....

Post by loicaigon »

I guess there is where starts the RegExp headaches ;)

Code: Select all

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;
Loic
http://www.ozalto.com
Danny
Member
Posts: 27
Joined: Wed Dec 08, 2010 5:59 pm

Re: Modify Script - Move File to Folder....

Post by Danny »

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?

Thanks again for your time
loicaigon
Member
Posts: 147
Joined: Wed Jul 10, 2013 10:22 am

Re: Modify Script - Move File to Folder....

Post by loicaigon »

Hi Danny,

I didn't test it nor I can do it right now but I can see what's wrong here. I didn't add the .name property while accessing the directory name.

Code: Select all

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;
Let me know if it's ok now. It should.

Loic
http://www.ozalto.com
Danny
Member
Posts: 27
Joined: Wed Dec 08, 2010 5:59 pm

Re: Modify Script - Move File to Folder....

Post by Danny »

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"

Any ideas??

Thanks again for your time!

Danny
loicaigon
Member
Posts: 147
Joined: Wed Jul 10, 2013 10:22 am

Re: Modify Script - Move File to Folder....

Post by loicaigon »

Hi,

That's what happens when you presume javascript core integration between apps :)

So now I have tried in real life and finally got it to work ;)

Code: Select all

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;
	
HTH,

Loic
http://www.ozalto.com
bens
Member
Posts: 130
Joined: Thu Mar 03, 2011 10:13 am

Re: Modify Script - Move File to Folder....

Post by bens »

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.

This brings us to:

Code: Select all

if ( reg.search( fo ) != -1 ) { /* ... */ }
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.

Code: Select all

folder.absFilePath( fo );
Danny
Member
Posts: 27
Joined: Wed Dec 08, 2010 5:59 pm

Re: Modify Script - Move File to Folder....

Post by Danny »

Thanks you both for your efforts, it's working great! I can't wait to implement this...

Thanks again for your help!
Post Reply