How do I save a file using javascript as a different name if file already exists?

How do I save a file in Acrobat Pro 10 using javascript as a different name if file already exists?

I have a javascript that is run when a button on a form is pressed. The javascript saves the file based on field names. Now I would like it to not overwrite if a file is already save with the same name. I would like it to just add an 1 to the end of the file name and then incrementally increasing.

the javascript:



// Get the field value
var fn = getField("Job Name").valueAsString + "_" + getField("Bill to").valueAsString + "_" + getField("Invoice#").valueAsString + "_Exported";
// Specify the folder
var fldr = "/c/PATHNAME/Exported/";
// Determine the full path
var fp = fldr + fn + ".pdf";
// Save the file
myTrustedSpecialTaskFunc(this, fp);

Thank you for any assistance.


Eric Stacy


3 Answers

Unfortunately, this cannot be done. For security reasons Acrobat JavaScript is sandboxed, meaning that is only has controlled, and weak, connections to the outside world. It cannot test for files in the file system.

Thom Parker
The source for PDF Scripting Info pdfscripting.com
All About PDF Stamps in Acrobat and Paperless Workflows - THE BOOK !!

The Acrobat JavaScript Reference, Use it Early and Often

The most important JavaScript Development tool in Acrobat
The Console Window (Video tutorial)
The Console Window(article)

Having trouble, Why Doesn't my Script Work


Thom Parker   

Actually, it is possible, but only if the other file is disclosed.
In that case you could try to open it using app.openDoc() and check if the result is null. If it is, you know there's no file with that name (or there is, but it isn't disclosed). If it's not null, you know there a disclosed file with that file name.
What you can do is add a script that discloses the files before you save them, to make sure that you'd be able to find them the next time you run the script.

-------------------------------------
Visit my custom-made PDF scripts website: http://try67.blogspot.com
Contact me personally: try6767@gmail.com


Gilad D (try67)   

Were you able to figure out how to do this? If so, can you provide an example script?


Brad Herbert   


Please specify a reason: