New job from existing file

Post Reply
patej
Member
Posts: 79
Joined: Sun Nov 25, 2012 12:15 pm

New job from existing file

Post by patej »

Hi
I am trying to get the total page count of all files in a zip file and without using external scripts only way I know of is to use job.getVariableAsNumber("[Stats.NumberOfPages]"), but that requires that the files are turned into jobs, doesn't it? I tried to implement that idea, but I can't figure out how I can create a job from an existing file.

I tried creating a new job and copying the existing file into a path defined to it, but still I can't get the Stats.NumberOfPages out from the file, Switch only says "The requested statistics are not available for the job 'C:/Users/Switchuser/AppData/Roaming/Enfocus/Switch Server/temp/EnvironmentClass/2145/_0A1OR_dummyFileForNewJob.dat': the job does not exist" --> how can I get the statistics out from the pdf files through the job? Or am I trying a too complicated way to solve this?

Here is (one example of) what I've tried:

Code: Select all

var file = job.getPath();
var unzipPath = job.createPathWithName( "unzipped", true );
var pageCount = 0;
if( !s.unarchive( file, unzipPath ) ){
	job.fail( "Failed to unarchive "+file );
	return;
}
else {
	var pdfDir = new Dir(unzipPath);
	var pdfFiles = pdfDir.entryList("*.pdf", Dir.Files, Dir.Name);
	for(var f=0;f<pdfFiles.length;f++){
		var tempJob = s.createNewJob();
		var filePath = tempJob.createPathWithName('pdfFile.pdf',false);
		s.copy(unzipPath+'/'+pdfFiles[f],filePath);
		pageCount += tempJob.getVariableAsNumber("[Stats.NumberOfPages]"); //this results in the error mentioned above
	}
	s.log(2,'page count:'+pageCount);
}
Any help is much appreciated,
Patrik
freddyp
Advanced member
Posts: 1022
Joined: Thu Feb 09, 2012 3:53 pm

Re: New job from existing file

Post by freddyp »

The reason your approach does not work is because the job is only created when the script finishes. The solution is to use the FileStatistics class. You can use that on any file, it does not have to be a job:

Code: Select all

var filestats = new FileStatistics(pathToFileAsString);
The rest is in the documentation of the class itself.
Post Reply