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

Participant ,
Oct 06, 2017

Copy link to clipboard

Copied

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.

Adobe Community Professional
Correct answer by Joel_Geraci | Adobe Community Professional

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.

TOPICS
Acrobat SDK and JavaScript

Views

625

Likes

Translate

Translate

Report

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

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

Participant ,
Oct 06, 2017

Copy link to clipboard

Copied

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.

Adobe Community Professional
Correct answer by Joel_Geraci | Adobe Community Professional

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.

TOPICS
Acrobat SDK and JavaScript

Views

626

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 06, 2017 0
Most Valuable Participant ,
Oct 06, 2017

Copy link to clipboard

Copied

Not possible using Acrobat JS.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 06, 2017 1
Participant ,
Oct 06, 2017

Copy link to clipboard

Copied

thanks

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 06, 2017 0
Adobe Community Professional ,
Oct 06, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 06, 2017 0
Most Valuable Participant ,
Oct 06, 2017

Copy link to clipboard

Copied

Nice one!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 06, 2017 0
Adobe Community Professional ,
Oct 06, 2017

Copy link to clipboard

Copied

Thanks but it's basically the same idea as the code they referenced. I can't take credit.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 06, 2017 0
Most Valuable Participant ,
Oct 06, 2017

Copy link to clipboard

Copied

It's better because it doesn't require opening (and displaying) the file, and can be used for non-PDF files as well.

Also, I believe the code above is not going to work if the file is not disclosed, although there might be ways around that.

So credit given!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 06, 2017 0
Adobe Community Professional ,
Oct 06, 2017

Copy link to clipboard

Copied

You actually can set the hidden flag when opening a PDF so that you can verify it's existence then close it without displaying it.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 06, 2017 0
Most Valuable Participant ,
Oct 06, 2017

Copy link to clipboard

Copied

I believe it's not working anymore, though.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 06, 2017 0
Adobe Community Professional ,
Oct 06, 2017

Copy link to clipboard

Copied

Well that's disappointing. [frowny-face emoticon]

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 06, 2017 0