Skip to main content
Participant
June 7, 2019
Question

How to ask to overwrite existing file?

  • June 7, 2019
  • 1 reply
  • 2080 views

Hi, currently I have a script I found that allows me to export with custom names etc. The script also checks if the file with same name already exists, and if it does, then it pops up an alert "file name already exists" Then it stops exporting.

I want to be able to continue to export and overwrite/replace the existing files.

I can't seem to figure out the Conditional statements with if, else, else if etc

This is the current code, any help would be great thanks!

   

                                        // Check if PDF-file allready exists  

                                        if (myFile.exists == false){  

                                                // Export the file  

                                                myDoc.exportFile(ExportFormat.pdfType, myFile, false, myPreset);  

                                        }else{  

                                                alert("PDF-file allready exists !");  

                                        }  

                            }  

This topic has been closed for replies.

1 reply

Mikie9
Participating Frequently
June 7, 2019

Hi David,

The code you have will overwrite an existing file but not inside the if statement that checks for an existing file like it currently is.

in my mind you have a couple of options. If you want the dialog box to remain, but after clicking OK over write the file, use this code.

// Check if PDF-file allready exists  

                                        if (myFile.exists == false){  

                                                // Export the file  

                                                myDoc.exportFile(ExportFormat.pdfType, myFile, false, myPreset);  

                                        }else{  

                                                alert("PDF-file allready exists !");

                                                myDoc.exportFile(ExportFormat.pdfType, myFile, false, myPreset);  

                                        }  

                            } 

If you just want it to over write the file without the dialog box, remove the whole block and just use the export line.

                                                myDoc.exportFile(ExportFormat.pdfType, myFile, false, myPreset);  

Participant
June 7, 2019

Thank you, however this only gives the okay to overwrite, but what if I want it to cancel?

Also this prompts the dialog up for each pdf exported. so if I have 7 pages, it asks me 7 times. How do I have it that it ask once and will apply to all?

Thanks

Community Expert
June 8, 2019

Hi David,

If you want a choice to move ahead with the export or cancel then you can use the confirm dialog instead of an alert. Something like the following

// Check if PDF-file allready exists 

if (myFile.exists == false)

     // Export the file 

     myDoc.exportFile(ExportFormat.pdfType, myFile, false, myPreset); 

}

else

     var a = confirm("PDF-file allready exists. Do you want to overwrite it?")

     if(a == true)

          myDoc.exportFile(ExportFormat.pdfType, myFile, false, myPreset); 

}

Regarding the message being showed in a loop we will need to have a look at the whole code to fix that.

-Manan

-Manan