Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

File Exists using folder level

Participant ,
Mar 08, 2016 Mar 08, 2016

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");   

    } 

TOPICS
Acrobat SDK and JavaScript
955
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 08, 2016 Mar 08, 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");

}

Translate
Community Expert ,
Mar 08, 2016 Mar 08, 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).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 08, 2016 Mar 08, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2016 Mar 08, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 08, 2016 Mar 08, 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");

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2016 Mar 08, 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");

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 08, 2016 Mar 08, 2016
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines