7 Replies Latest reply on Dec 16, 2014 6:14 AM by try67

    How to scan a specific folder for the existance of a PDF before saving

    MatLac Level 1

      I created a folder level function for saving a file by passing it a name and a path.  The problem is I don't want to allow the user to overwrite a file of the same name.  I would rather want to append a version number to it's name so I can keep track of the evolution of the form.

       

      I'm thinking about a script to scan the folder for the occurrence of a file and returns something or throws an error.  I experimented with app.browseForDoc() but I don't want the dialog box to show.

       

      I would use a do/while loop to appends a version number and then look for the file until I get a specific return value or error indicating that the file is not present (or in my case that the name is not already used).

        • 1. Re: How to scan a specific folder for the existance of a PDF before saving
          try67 MVP & Adobe Community Professional

          You can try to open the file... If you succeed then you know it exists (the

          file has to be disclosed for that to work, though).

          If you do that in a loop then you can find the last file created and save

          your file as the next one in the series.

           

          You can't scan the contents of a folder using a script.

          • 2. Re: How to scan a specific folder for the existance of a PDF before saving
            MatLac Level 1

            The way I see it then is I should include the bPromptToOverwrite parameter in my save function to at least prevent an automatic overwriting and then, use the NotAllowedError that is thrown when disallowing to prompt him to append a number version to its file name.

             

            It's not perfect but I cnnot afford to open onother PDF just to see if it exists as our network is over cellular waves and quite slow.

            • 3. Re: How to scan a specific folder for the existance of a PDF before saving
              try67 MVP & Adobe Community Professional

              Not sure that will work, but it's worth a try...

              • 4. Re: How to scan a specific folder for the existance of a PDF before saving
                MatLac Level 1

                I think I have just the answer, I still need to try it though.  I forgot about prompting the user for appending version numbers because it would mean he'd have to cancel a bPromptToOverwrite box for each version present in the folder.  That's way too many boxes.

                 

                BUT....;o)

                 

                I needed a way to prevent overwriting which is the first concern.  What I'm looking for is a way to:

                1-stop my save script before overwiting;

                2-Append version number if script stops and launch save script again;

                 

                One way to stop the script is when an error is thrown.  I'm thinking about saving the file as a copy with the bCopy parameter and adding a DOS script to the folder that would monitor in real time the attributes of all files and turn it to read-Only.  That way, if I run my save function and reader tries to overwrite a file, it will throw an error.  I will modify my save function to return a boolean named successfulSave.  Using a try/catch handler, I will append a version number to the file name and call the save function again.  I will encapsulate this in a do/while loop until successfullSave is true.

                • 5. Re: How to scan a specific folder for the existance of a PDF before saving
                  MatLac Level 1

                  check this out

                   

                  var notSaved = true;

                  var caseNumber = this.getField("CaseNumber").valueAsString;

                  var i = 1;

                   

                  while (notSaved){

                    try{

                    var myPathEnd = "LGM-"+caseNumber+"("+i+").pdf";

                    this.saveAs({cPath:"C:\\Users\\mlacass9\\Desktop\\"+myPathEnd, bCopy:true});

                    notSaved = false;

                    } catch (e){

                    if (e.name == "RaiseError") i++;

                    }

                  }

                   

                  A tested it from the console and it works.  Of course it is not perfect as something else could be creating a RaiseError and I would then be stuck in an infinite loop.  I will update the while condition to a maximum of some "versions" before prompting the user with an "unusual Error" and permit him to manually terminate the script.  I guess this is the best work around so far providing my IT departement can deploy the DOS script I'm expecting.

                  • 6. Re: How to scan a specific folder for the existance of a PDF before saving
                    MatLac Level 1

                    Finally, a much simpler implementation was to add a simple time stamp to the name of the file when saving, It prevents from overwritng & as a bonus, it keeps a track of the evolution of the file over time.

                    • 7. Re: How to scan a specific folder for the existance of a PDF before saving
                      try67 MVP & Adobe Community Professional

                      Sure, that's a much easier way of doing it, if you don't mind that you end up with multiple copies of the same file in your folder.