Page 1 of 1

File Directory in Javascript

Posted: Thu Mar 20, 2014 8:33 pm
by PaulHough
So I have been working on a script that will pull old job folders from several directories based on the job number passed from switch. Everything is working great, but I ran into a snag. Some of the folders are named with a "_Customer Name" and some are not. The script works as is should for the folders WITHOUT the "_customer Name" but fails to retrieve the folders WITH "_Customer Name".





What I Assume I should do is get a directory of each path (var locObj[] ) then do a match against my job Number. but, i can not for the life of me figure out how to get a directory out of javascript.



Or, Am i over thinking it?





Attached is the current script



// Is invoked each time a new job arrives in one of the input folders for the flow element.

// The newly arrived job is passed as the second parameter.

function jobArrived( s : Switch, job : Job )

{

//Total Number of Locations//

var NumberOfLocations = 22;

//Location Paths//



var LocObj = [];

LocObj['Location1']= "/Volumes/prep_files/Jobs/";

LocObj['Location2']= "/Volumes/prepress_files/Archived Jobs/• Xitron_Backups/";

LocObj['Location3']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1016001 - 1017000/";

LocObj['Location4']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1015001 - 1016000/";

LocObj['Location5']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1014001 - 1015000/";

LocObj['Location6']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1013001 - 1014000/";

LocObj['Location7']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1012001 - 1013000/";

LocObj['Location8']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1011001 - 1012000/";

LocObj['Location9']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1010001 - 1011000/";

LocObj['Location10']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1009001 - 1010000/";

LocObj['Location11']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1008001 - 1009000/";

LocObj['Location12']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1007001 - 1008000/";

LocObj['Location13']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1006001 - 1007000/";

LocObj['Location14']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1005001 - 1006000/";

LocObj['Location15']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1004001 - 1005000/";

LocObj['Location16']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1003001 - 1004000/";

LocObj['Location17']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1002001 - 1003000/";

LocObj['Location18']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1001001 - 1002000/";

LocObj['Location19']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1000000 - 1001000/";

LocObj['Location20']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/500000 (Online Orders)/";

LocObj['Location21']= "/Volumes/prepress_files/DONTUSE/"

LocObj['Location22']= "/Volumes/prepress_files/Transfer/• Prepress/Prepress_Files_Programs/Switch Backup/NoJob";





// Switch metadata with history number//

var JobNumber = job.getVariableAsString('[Metadata.Text:Path="/field-list/field[2]/field-list/field/value",Dataset="Submit",Model="XML"]');

//I have no Idea what this is its from fred//

var theSourcePath = job.createPathWithName( JobNumber );

//Counter for loop//

var Counter = 1;

// location path test: True=nofile False=FileFound

var Empty = "True";

//Loop and search locations for history number//

while(Empty == "True")

{

var shortcut = LocObj['Location'+Counter+'']

//var path = Location1+Counter;

var folderName = shortcut+JobNumber;

s.log(1,folderName);

if (File.exists(folderName))

{

Empty = "False";

s.copy( folderName, theSourcePath );

job.sendToSingle( theSourcePath );

job.sendToNull( job.getPath() );

s.log(1,JobNumber);

}

//Break loop is no files are found//

else if(Counter == NumberOfLocations)

{

var theSourcePath = job.createPathWithName( "NoJob" );

var folderName = shortcut;

s.copy( folderName, theSourcePath );

job.sendToSingle( theSourcePath );

job.sendToNull( job.getPath() );

s.log(1,"Folder " + folderName + " does not exist");

Empty = "False";

}

else

{

Counter++;

}

}



}




File Directory in Javascript

Posted: Thu Mar 20, 2014 10:11 pm
by dkelly
var theDir = new Dir("/directoryname");

var theEntries = theDir.entryList("*", Dir.Files, Dir.Name);

for (var i=0; i<theEntries.length; i++) {

var fileName = theDir.absPath + "/" + theEntries;

}

File Directory in Javascript

Posted: Fri Mar 21, 2014 8:10 pm
by dkelly
This script expression will return all of the subdirectory names in "/Server/files" directory.



var theDir = new Dir("/Server/files");

var theEntries = theDir.entryList("*", Dir.Dirs, Dir.Name);

var list = new Array;

for (var i = 0; i < theEntries.length; i++) {

if (theEntries == '.' || theEntries == "..")

continue;

list.push(theEntries);

}

list;





Dwight Kelly

Apago, Inc.

Switch reseller and script developer

File Directory in Javascript

Posted: Tue Mar 25, 2014 6:19 pm
by PaulHough
Thanks for the help.



Attached is the script for future reference to anyone who might need it.





// Is invoked each time a new job arrives in one of the input folders for the flow element.

// The newly arrived job is passed as the second parameter.

function jobArrived( s : Switch, job : Job )

{

//Total Number of Locations//

var NumberOfLocations = 22;

//Location Paths//



var LocObj = [];

LocObj['Location1']= "/Volumes/prep_files/Jobs/";

LocObj['Location2']= "/Volumes/prepress_files/Archived Jobs/• Xitron_Backups/";

LocObj['Location3']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1016001 - 1017000/";

LocObj['Location4']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1015001 - 1016000/";

LocObj['Location5']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1014001 - 1015000/";

LocObj['Location6']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1013001 - 1014000/";

LocObj['Location7']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1012001 - 1013000/";

LocObj['Location8']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1011001 - 1012000/";

LocObj['Location9']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1010001 - 1011000/";

LocObj['Location10']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1009001 - 1010000/";

LocObj['Location11']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1008001 - 1009000/";

LocObj['Location12']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1007001 - 1008000/";

LocObj['Location13']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1006001 - 1007000/";

LocObj['Location14']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1005001 - 1006000/";

LocObj['Location15']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1004001 - 1005000/";

LocObj['Location16']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1003001 - 1004000/";

LocObj['Location17']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1002001 - 1003000/";

LocObj['Location18']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1001001 - 1002000/";

LocObj['Location19']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/1000000 - 1001000/";

LocObj['Location20']= "/Volumes/prepress_files/Archived Jobs/• Prinergy_Backups/500000 (Online Orders)/";

LocObj['Location21']= "/Volumes/prepress_files/DONTUSE/"

LocObj['Location22']= "/Volumes/prepress_files/Transfer/• Prepress/Prepress_Files_Programs/SwitchBackup/NoJob";





// Switch metadata with history number//

var JobNumber = job.getVariableAsString('[Metadata.Text:Path="/field-list/field[2]/field-list/field/value",Dataset="Submit",Model="XML"]');

//Define Regual Expresion



//I have no Idea what this is its from fred//

var theSourcePath = job.createPathWithName( JobNumber );

//Counter for loop//

var Counter = 1;

// location path test: True=nofile False=FileFound

var Empty = "True";

//Loop and search locations for history number//

while(Empty == "True")

{

//Create Directory of files

var theDir = new Dir(LocObj['Location'+Counter+'']);

var theEntries = theDir.entryList("*");

var longFolderName='';

var needle = JobNumber;

var re = new RegExp(needle, "g");

for(var i=0; i<theEntries.length;i++){

var folder = theEntries;

if(folder.match(re))

{

longFolderName+=theEntries;

}

}

var shortcut = LocObj['Location'+Counter+'']

//var path = Location1+Counter;



if (longFolderName != "")

{

Empty = "False";

var folderName = shortcut+longFolderName;

s.copy( folderName, theSourcePath );

job.sendToSingle( theSourcePath );

job.sendToNull( job.getPath() );

s.log(1,longFolderName);

}

//Break loop is no files are found//

else if(Counter == NumberOfLocations)

{

var theSourcePath = job.createPathWithName( "NoJob" );

var folderName = shortcut;

s.copy( folderName, theSourcePath );

job.sendToSingle( theSourcePath );

job.sendToNull( job.getPath() );

s.log(1,"Folder " + folderName + " does not exist");

Empty = "False";

}

else

{

Counter++;

}

}



}

File Directory in Javascript

Posted: Wed Mar 26, 2014 11:27 am
by tz8
dkelly wrote: This script expression will return all of the subdirectory names in "/Server/files" directory.



Dwight Kelly

Apago, Inc.

Switch reseller and script developer


var theDir = new Dir("/Server/files");

var theEntries = theDir.entryList("*", Dir.Dirs|Dir.NoDotAndDotDot, Dir.Name);

var list = new Array;

for (var i = 0; i < theEntries.length; i++)

list.push(theEntries);

list;





i shortened your code to get rid of this "." and ".." test which is not needed when using the Dir.NoDotAndDotDot filter



Cheers,



Thorsten