Skip to main content
jeremy0013
Inspiring
March 8, 2016
Answered

File Exists using folder level

  • March 8, 2016
  • 3 replies
  • 1125 views

Need some help here.  I am writing some javascript on a acrobat form using a folder level script to see if a file exists.  This does not seem to work for me for some reason.  If don't have LiveCycle will the code below still work?  Is there a setting I am missing?

Folder Level

var IfFileExists = app.trustedFunction(function(filename) {

    app.beginPriv(); 

    var existingDoc = false; 

    try { 

         var checkDoc = app.openDoc(filename); 

         checkDoc.closeDoc(); 

     existingDoc = true; 

    } catch(e) { 

     existingDoc = false; 

    } 

    return existingDoc; 

    app.endPriv(); 

}); 

Doc Level

var onumber = this.getField("info_Order#");

var FileDir = ("/Volumes/PFS/" + onumber.value + ".fdf");

if(typeof(IfFileExists) == "function") { 

    try { 

        if(IfFileExists(FileDir) == true) { 

            app.alert("file is there"); 

        } else { 

            app.alert("file is NOT there"); 

        } 

    } 

    catch(e) { 

        app.alert("file is NOT there");   

    } 

This topic has been closed for replies.
Correct answer try67

This function doesn't return true/false. It returns a Stream object or throws an exception, so use it like this:

try {

    util.readFileIntoStream(FileDir);

    app.alert("The file is there");

} catch (e) {

    app.alert("The file is NOT there");

}

3 replies

jeremy0013
Inspiring
March 9, 2016

Thanks.  This works.  The try and catch is new to me. Thanks for all your help.

jeremy0013
Inspiring
March 8, 2016

I tried this but it doesn't work.  It skips to the else argument?

var onumber = this.getField("info_Order#");

var FileDir = ("/Volumes/PFS/" + onumber.value + ".fdf");

stmData = util.readFileIntoStream(FileDir);

if (stmData == true) {

app.alert("The file is there");

} else {

app.alert("The file NOT there");

}

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
March 8, 2016

This function doesn't return true/false. It returns a Stream object or throws an exception, so use it like this:

try {

    util.readFileIntoStream(FileDir);

    app.alert("The file is there");

} catch (e) {

    app.alert("The file is NOT there");

}

try67
Community Expert
Community Expert
March 8, 2016

Your IfFileExists method looks like it can work, but only if the file is disclosed. Also, keep in mind it will only work with PDF files, not FDF files or any other file type (unless you convert that file to PDF).

jeremy0013
Inspiring
March 8, 2016

Thanks for the quick reply.  New to Acrobat scripting.  The files are all FDF files, so this will probably not work.  Maybe I need to setup a database to use this method.  Can you point me in the right direction?

try67
Community Expert
Community Expert
March 8, 2016

You can't program databases in Acrobat JS.

You can use the importAnFDF command, but if you specify to it a non-existing file-path it will cause an Open dialog to appear, which might not work for you...

Another option is to use util.readFileIntoStream(). It will return a Stream object if successful, or will throw an Exception if the file doesn't exist.