Sorting Files (jobs) in numerical order with script

Post Reply
wstevensharp
Newbie
Posts: 4
Joined: Mon Oct 31, 2011 10:01 pm

Sorting Files (jobs) in numerical order with script

Post by wstevensharp »

We have a flow that takes a single pdf and splits into a file per page. We add the page number to the file name and must have them in page order at the end of the flow. Switch doesn't keep them in order when splitting.



Trying to get a script to sort (that works fine) refresh the arrival stamp and send the file on to the next folder in the flow.



Can not get the file (job) to advance.

Here's what we have:



function jobArrived( s : Switch, job : Job ) {



var version = "V11: ";

var scope = "Sharp_Concat_Page_Jobs";

var fullPathAndFileName = job.getPath();

job.log(1, "fullPathAndFileName " + fullPathAndFileName);





var fullPathAndFileNameSplit = fullPathAndFileName.split("/_");

var pathOnly = fullPathAndFileNameSplit[0];



// get file name without unique prefix or file extension

var fileName = job.getNameProper();

job.log(1, "NameProper " + fileName);

// remove beginning "_" if present

if (fileName.indexOf("_") == 0) {

fileName = fileName.substr(1);

}



// split file name so we can get the group name

var fileNameSplit = fileName.split("_");

var groupName = fileNameSplit[0];

job.log(1, "group name " + groupName);

var totalPages = fileNameSplit[4];

job.log(1, "total pages " + totalPages);

var targetPage = s.getGlobalData(scope, groupName);

job.log(1, "target page " + targetPage);



if (targetPage == "") {

job.log(1, version + "Initializing global variable.");

targetPage = 1;

s.setGlobalData(scope, groupName, targetPage);

job.log(1, "target page after reset " + targetPage);

}



do {



var directory = new Dir(pathOnly);

var fileList = directory.entryList("*.pdf", Dir.Files, Dir.Name);

var fileListCount = fileList.length;

job.log(1, "list count " + fileListCount);



if (fileListCount != 0) {



for (i=0; i < fileListCount; i++) {

var fileName = fileList;

// job.log(1, "fileName " + fileName);

var fileNameSplit = fileName.split("_");

var fileGroup = fileNameSplit[2];

// job.log(1, "fileNameSplit Group " + fileGroup);

var filePage = fileNameSplit[4];

//job.log(1, "fileNameSplit Page " + filePage);

//job.log(1, "Value of i " + i );



if (fileGroup == groupName && filePage == targetPage) {

job.log(1, version + "Found a match!");

job.refreshArrivalStamp();

job.log( 1, version + "New time stamp = " + job.getArrivalStamp() );

job.log(1, version + "File name to advance " + fileList );

job.sendToSingle(fileList);

targetPage++;

s.setGlobalData(scope, groupName, targetPage);

job.log(1, version + "Processed file! Searching for " + targetPage + " next.");





}

}

}

} while (targetPage != totalPages);



if (targetPage == totalPages) {

// reset global variable back to 1 for next run



s.setGlobalData(scope, groupName, 1);

}





Thanks
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Sorting Files (jobs) in numerical order with script

Post by dkelly »

Hello, this isn't a complete drop-in solution since I don't know exactly how your filenames are constructed - but hopefully it helps.



function pageNumCompare(a, b)

{

return a.pageNum - b.pageNum;

}



function addFilePage(theArray, filename, pagenum)

{

var fp = new Object();

fp.fileName = filename;

fp.pageNum = pagenum;

theArray.push(fp);

}



function jobArrived( s : Switch, job : Job )

{

var myFilePageArray = new Array();



var path = job.getPath();

job.log(1, "path: " + path);

var directory = new Dir(path);

var fileList = directory.entryList("*.pdf", Dir.Files, Dir.Name);

var fileListCount = fileList.length;

job.log(1, "file count: " + fileListCount);



var i;

for (i=0; i < fileListCount; i++) {

var fileName = fileList;

// job.log(1, "fileName: " + fileName);

var fileNameSplit = fileName.split("_"); // 0_1_fileGroup_3_filePage_5.pdf

var filePage = fileNameSplit[4];

job.log(1, """ + filePage + "" - " + parseInt(filePage));

addFilePage(myFilePageArray, fileName, parseInt(filePage));

}



job.log(1, "Sorting...");

myFilePageArray.sort(pageNumCompare);



// sent to next flow element

job.log(1, "Sorted list");

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

job.log(1, myFilePageArray.fileName);

job.sendToSingle(path + "/" + myFilePageArray.fileName);

}

}



Dwight Kelly

Apago, Inc.

dkelly@apago.com
wstevensharp
Newbie
Posts: 4
Joined: Mon Oct 31, 2011 10:01 pm

Sorting Files (jobs) in numerical order with script

Post by wstevensharp »

Thank you Dwight, a big help.
Post Reply