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

How to ask to overwrite existing file?

Community Beginner ,
Jun 07, 2019 Jun 07, 2019

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

                                        }  

                            }  

TOPICS
Scripting
2.1K
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
Explorer ,
Jun 07, 2019 Jun 07, 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);  

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 Beginner ,
Jun 07, 2019 Jun 07, 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

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 ,
Jun 07, 2019 Jun 07, 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

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 ,
Jun 08, 2019 Jun 08, 2019

Hi Manan,

also consider the case where the already written PDF is opened with e.g. Acrobat and the file cannot be overwritten.

Regards,
Uwe

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 ,
Jun 08, 2019 Jun 08, 2019
LATEST

Thanks Uwe, for pointing out this use case. Well in this case i think we will have to enclose our export call within a try catch block and report the issue which will include any other case which might result in overwrite failure, unless these is any other cleaner way to deal with this.

-Manan

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
Advisor ,
Jun 07, 2019 Jun 07, 2019

David,

The "confirm" dialog will allow you to choose to continue or cancel if the file exist.

Not sure about "How do I have it that it ask once and will apply to all?" with out seeing the rest of your code.

// Check if PDF-file allready exists

if (myFile.exists == false){

// Export the file

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

}else{

var result = confirm("PDF-file allready exists!\rYes to Continue or No to Cancel.", false);

if (result == true){

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

}

}

Regards

Mike

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