Sorting Files (jobs) in numerical order with script
Posted: Mon Oct 31, 2011 10:19 pm
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
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