These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

How can I determine if a PDF file exist?

Mark Green
Registered: Mar 29 2009
Posts: 19
Answered

I am writng a script to allow my users to combine up to 8 pdfs into the open document. The script needs to determine which of the 8 files are available and then insert them in a preset order.
The files are always in the same directory and are a branch name of the main file
EG Main_File.pdf
Then Main_File_A.pdf, Main_File_B.pdf, Main_File_D.pdf etc
As you will note Main_File_C.pdf was not listed as this may not have been created so I need my script to identify it is not there and skip over it.

I have tried playing with the catching the exception error on an app.opendoc() but it does not suit my purpose. I dont want the files to open just to know they are there.

I can work through the code to insert etc I really just need a pointer to the problem of determining whether a file exists

Thanx in advance anyone who can Help
Mark Green

My Product Information:
Acrobat Pro 8.1, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
There's no direct way of verifying if a file exists in Acrobat's JS. I think the app.openDoc() method is your best chance. You can try to open the file using openDoc, hide it, and then immediately close it. If no exception is thrown then the file exists. Otherwise, just dismiss the exception and continue.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
JavaScript has a [url=https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/try...catch]'try...catch'[/url] statement that allows one to run possible error producing code and catch the error if thrown. One can then drop the error into the bit bucket or handle the problem as needed.

// try to open an html doc on user's hard drive - supress the errortry {app.openDoc("/c/myWeb/myHomePage.php");} catch (e) {// error goes  to the bit bucket}

// Print all properties of the Error object to the console.try {app.alert(); // one argument is required for alert}  // end try// catch the errorcatch(e) {// print message textconsole.println(e.toString() + '\n');// print all the properties of the error messageconsole.println('Details of the error message:');for (var i in e) {console.println( i + ": " + e[i]);} // end for individual properties loop } // end catch error

George Kaiser

Mark Green
Registered: Mar 29 2009
Posts: 19
Thanx as usual for your responses I have used the Try Catch method and had soome success but failing when trying to open documents from an array.

Code Below; (Excuse my newbiew code writing)

function Gtest(cName, nPage)
{
aFiles = new Array("_Map_A.pdf","_Map_B.pdf","_Map_C.pdf","_Map_D.pdf","_Map_E.pdf","_Demo.pdf","_Bus.pdf","_Spend.pdf");
var aPathComps = this.path.split("/"); //Path
var myFileName = aPathComps.pop(); //Filename
var fileNmRoot = myFileName.split(".").shift(); //Root Filename
var strNewPath = aPathComps.join("/"); // Append path to File to Insert

for ( var i=0; i < aFiles.length; i++) { //Loop through Array
var fileNmNew = fileNmRoot + aFiles[i] // File to Insert
console.println(fileNmNew);
var cnewfile = strNewPath + "/" + fileNmNew // Combine All
console.println(cnewfile);
}
for ( var i=0; i < aFiles.length; i++) {
var iffileexists = 1;
try{

//*********************
// This is where it starts to fall over?
// it will not open the documentsd from the array
//**********************

var otherDoc = app.openDoc(cnewfile[i]);
}catch(e){
iffileexists = 0;
}
if (iffileexists = 1)
{
otherDoc.closeDoc();
this.insertPages({nPage:this.numPages-1,cPath:cnewfile[i]});
}
}}
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Try printing cnewfile[i] to the console and make sure that you got the path right.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Mark Green
Registered: Mar 29 2009
Posts: 19
Already tried that and paths are correct

Mark Green
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Could you maybe post one of them here?
Also, this line is incorrect:
if (iffileexists = 1)
You're using the assignment operator instead of the comparison one.
But that should not cause this problem.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Mark Green
Registered: Mar 29 2009
Posts: 19
/C/AAA_Pdf/Hexham_5k_Map_A.pdf
/C/AAA_Pdf/Hexham_5k_Map_B.pdf
/C/AAA_Pdf/Hexham_5k_Map_C.pdf
/C/AAA_Pdf/Hexham_5k_Map_D.pdf

Mark
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Have you set the 'disclosed' property of the PDF's being opened to true?

This document property was added in version 5.05 and is required to be 'true' in a PDF being opened by JavaScript. See the Acrobat JS API Reference for more information.

George Kaiser

Mark Green
Registered: Mar 29 2009
Posts: 19
Not sure about the disclosed property but I have re-written the code to open each file individually (not through the array loop) and it works fine opens all the documents.
It is only when I try to open them the array loop that it fails.

For my own purposes the second script I wrote is fine, but as there does not seem to be any other tools around to perform this task I thought having an array loop is much cleaner and might be useful for other users.

New Program that works: *********************

var aPathComps = this.path.split("/"); //Path
var myFileName = aPathComps.pop(); //Filename
var fileNmRoot = myFileName.split(".").shift(); //Root Filename
var strNewPath = aPathComps.join("/"); // Append path to File to Insert
//*** Map_A
var fileNmNew = fileNmRoot + "_Map_A.pdf" // File to Insert
var cnewfile = strNewPath + "/" + fileNmNew // Combine All
try{
var nfileexists = 1
var otherDoc = app.openDoc(cnewfile);
}catch(e){
nfileexists = 0;
}
if (nfileexists > 0)
{
otherDoc.closeDoc();
this.insertPages({nPage:this.numPages-1,cPath:cnewfile});
}
*********************** This then goes on and does Map_B, Map_C etc and works fine

Code that I cannot get to work:********************

{
aFiles = new Array("_Map_A.pdf","_Map_B.pdf","_Map_C.pdf","_Map_D.pdf","_Map_E.pdf","_Demo.pdf","_Bus.pdf","_Spend.pdf");

var aPathComps = this.path.split("/"); //Path
var myFileName = aPathComps.pop(); //Filename
var fileNmRoot = myFileName.split(".").shift(); //Root Filename
var strNewPath = aPathComps.join("/"); // Append path to File to Insert

for ( var i=0; i < aFiles.length; i++) { //Loop through Array
var fileNmNew = fileNmRoot + aFiles[i] // File to Insert
console.println(fileNmNew);
var cnewfile = strNewPath + "/" + fileNmNew // Combine All
console.println(cnewfile);
}
for ( var i=0; i < aFiles.length; i++) {
var iffileexists = 1;
try{
var otherDoc = app.openDoc(cnewfile[i]);
}catch(e){
iffileexists = 0;
}
if (iffileexists > 0)
{
otherDoc.closeDoc();
this.insertPages({nPage:this.numPages-1,cPath:cnewfile[i]});
}
}

}

Hope someone can help !

Thanx

Mark
try67
Expert
Registered: Oct 30 2008
Posts: 2398
The problem with your array code is that you never define "cnewfile" as an array. In the first loop you use it as a string and in the second loop you're trying to access it as an array. You should either define it as an array and push the file locations in the first loop and then access them in the second, or just combine the two into a single loop which will create the file name and try to open the file in the same iteration. I think the latter is better since it's more efficient (1 loop instead of 2).

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Mark Green
Registered: Mar 29 2009
Posts: 19
Thanx Try67 got it working with your tips
I could make it a little more elegant but for m needs it works and I am happy

Thanx everyone for your tips and help

Mark

Have attached code in case anyones interested

// Code to determione if a files exists then insert it into the open document
function GAddDP()
{
var aFiles = new Array("_Map_A.pdf","_Map_B.pdf","_Map_C.pdf","_Map_D.pdf","_Map_E.pdf","_Demo.pdf","_Bus.pdf","_Spend.pdf");
var cnewfile = new Array(8)

var aPathComps = this.path.split("/"); // Path
var myFileName = aPathComps.pop(); // Filename
var fileNmRoot = myFileName.split(".").shift(); // Root Filename
var strNewPath = aPathComps.join("/"); // Append path to File to Insert

for ( var i=0; i < aFiles.length; i++) { // Loop through Array
var fileNmNew = fileNmRoot + aFiles[i] // File to Insert
//console.println(fileNmNew);
cnewfile[i] = strNewPath + "/" + fileNmNew // Combine All
//console.println(cnewfile[i]);
}
for ( var i=0; i < cnewfile.length; i++) { // Loop through the list of filenames
var nfileexists = 1;
try{
var otherDoc = app.openDoc(cnewfile[i]); // Test if File exists
}catch(e){
nfileexists = 0; // Catch the errror if it doesnt
}
if (nfileexists > 0)
{
otherDoc.closeDoc(); // If it exists close it
this.insertPages({nPage:this.numPages-1,cPath:cnewfile[i]}); // Insert the document if it exists
}
}
}
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Glad to hear it works. I do have one suggestion for you, though.
Imagine that in the future you will add or remove files from your list. You will then have to remember to manually change the definition of cnewfile or your script will not work properly/at all.
To prevent this you should drop the length definition for this array (just remove the number 8 from the brackets), and instead of this line:

cnewfile[i] = strNewPath + "/" + fileNmNew

use this:

cnewfile.push(strNewPath + "/" + fileNmNew)

This will make the length of your array dynamic. The rest of the script should work just fine.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com