Skip to main content
PasGlop
Inspiring
October 6, 2017
Answered

how to check if a file exists without open and close it

  • October 6, 2017
  • 1 reply
  • 2883 views

Hello,

I found this topic (How to check if file exists? ) but I would like to do the same, without trying to open the file (the file type I want to check is not ".pdf" but can be anything else.

Here are the found scripts (thanks to tehinternets !)

Call the function :

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

  •     try { 
  •         if(MJS_FileExists("/C/something/somethingelse/filetocheckfor.pdf") == true) { 
  •             app.alert("file is there"); 
  •         } else { 
  •            app.alert("file is NOT there"); 
  •         } 
  •     } 
  •     catch(e) { 
  •         app.alert("Processing error: "+e) 
  •     } 

Function to place at the folder level :

var MJS_FileExists = 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(); 
  • }); 

Please advise,

Thanks and best regards.

This topic has been closed for replies.
Correct answer Joel Geraci

While try67 is technically correct, you can attempt to attach the file as a data object. If it fails, the file doesn't exist. There is enough sample code in the API documentation to modify the code you found to try adding an attachment.

1 reply

try67
Community Expert
Community Expert
October 6, 2017

Not possible using Acrobat JS.

PasGlop
PasGlopAuthor
Inspiring
October 6, 2017

thanks

Joel Geraci
Community Expert
Joel GeraciCommunity ExpertCorrect answer
Community Expert
October 6, 2017

While try67 is technically correct, you can attempt to attach the file as a data object. If it fails, the file doesn't exist. There is enough sample code in the API documentation to modify the code you found to try adding an attachment.