10 Replies Latest reply on Oct 14, 2013 9:33 AM by jessevic

    Read character 3+4 from filename and move to other position

    jessevic Level 1

      Hello,

       

      I need a javascript to read my filename (I have the one for the full filename) and especially the 3rd and 4th position of the filename.

      The characters on position 3 and 4 must be moved to the position just before the extension.

       

      Example:

      4PEN12345.pdf (old name) should be 4P12345_EN.pdf

       

      Now I have the following script to extract all pages in an existing pdf file to separate pages with all the same suffix before the extension

       

      /* Extract Pages to Folder */

          var re = /.*\/|\.pdf$/ig;
          var filename = this.path.replace(re,"");

          {
              for ( var i = 0;  i < this.numPages; i++ )
              this.extractPages

               ({
                  nStart: i,
                  nEnd: i,
                  cPath : filename + "_page_" + (i+1) + "_EN.pdf"
              });
          };

       

      Can anybody help me to set me on the right direction?

        • 1. Re: Read character 3+4 from filename and move to other position
          try67 MVP & Adobe Community Professional

          This code should do the trick. You use newFilePath at the end as the cPath parameter of extractPages:

           

          var filePath = this.path.replace(this.documentFileName, "");

          var oldFileName = this.documentFileName;

          var suffix = oldFileName.substring(2,4);

          var newFileName = oldFileName.substring(0,2) + oldFileName.substring(4).replace(".pdf", "_"+suffix+".pdf");

          var newFilePath = filePath + newFileName;

          • 2. Re: Read character 3+4 from filename and move to other position
            jessevic Level 1

            I'm keep struggling with the "cPath" item for the extraction of the pages.

            Can you please give more instructions?

            What also have to keep in mind is the following: in my example, the filename is very short (5 characters after the 4P to create the filename). It could also be 8 or 10 characters.

             

            Examples:

            4PEN12345_1.pdf

            4PDE1234567_1A.pdf

             

            The language code must be on the position before the extension, no matter how long the filename is.

             

            Kindly regards

            • 3. Re: Read character 3+4 from filename and move to other position
              try67 MVP & Adobe Community Professional

              With the code I provided it doesn't matter how long the file name is (assuming it's not shorter than 4 characters, of course).

               

              What's not clear about cPath? It's a string that represents the file path (in PDF syntax) to which the extracted page(s) are saved.

              • 4. Re: Read character 3+4 from filename and move to other position
                jessevic Level 1

                Now I have the file 4PBG12345-1.pdf (7 pages included in the pdf).

                 

                Your script as I filled in:

                 

                /* Extract Pages to Folder */

                    var re = /.*\/|\.pdf$/ig;
                    var filePath = this.path.replace(this.documentFileName, "");
                    var oldFileName = this.documentFileName;
                    var suffix = oldFileName.substring(2,4);
                    var newFileName = oldFileName.substring(0,2) + oldFileName.substring(4).replace(".pdf", "_"+suffix+".pdf");
                    var newFilePath = filePath + newFileName;

                    {
                        for ( var i = 0;  i < this.numPages; i++ )
                        this.extractPages

                         ({
                            nStart: i,
                            nEnd: i,
                            cPath : newFilePath
                        });
                    };

                 

                 

                Result:

                1 file ==> 4P12345-1_BG.pdf (7 pages included in the pdf)

                 

                Of course, because the syntax for extracting and adding automatic pages is not filled in. So, that was my question, how must I nest to add the right:

                Filepath+oldfilenamesubstring(0,2)+oldfilenamesubstring(4)+_page_i++_+suffix+.pdf in the extract section

                 

                So, my example must result in 7 pdf's:

                4P12345-1_page_1_BG.pdf (with content of page 1)

                4P12345-1_page_2_BG.pdf (with content of page 2)

                4P12345-1_page_3_BG.pdf (with content of page 3)

                4P12345-1_page_4_BG.pdf (with content of page 4)

                4P12345-1_page_5_BG.pdf (with content of page 5)

                4P12345-1_page_6_BG.pdf (with content of page 6)

                4P12345-1_page_7_BG.pdf (with content of page 7)

                • 5. Re: Read character 3+4 from filename and move to other position
                  try67 MVP & Adobe Community Professional

                  Ah, you didn't mention that you wanted the page number in there as well...

                  In that case, move the last two lines of the code into the for-loop, and change them to this:

                   

                  var newFileName = oldFileName.substring(0,2) + oldFileName.substring(4).replace(".pdf", "_page" + (i+1) + "_"+suffix+".pdf");

                  var newFilePath = filePath + newFileName;

                  • 6. Re: Read character 3+4 from filename and move to other position
                    jessevic Level 1

                    Yes, here is the solution and many thanks!

                    Only 1 remark: Each time I get the pop up if I want to save the original file. Is there a possibility to set this message of?

                    I was thinking that the ...replace.... part in the code was the cause, but it seems not I think.

                     

                     

                    Suggestions?

                     

                     

                     

                    /* Extract Pages to Folder */

                        var oldFileName = this.documentFileName;
                        var suffix = oldFileName.substring(2,4);
                        var filePath = this.path.replace(this.documentFileName, "");

                        {
                            for ( var i = 0;  i < this.numPages; i++ )
                            this.extractPages
                          
                           
                             ({
                                nStart: i,
                                nEnd: i,
                                cPath : filePath+oldFileName.substring(0,2) + oldFileName.substring(4).replace(".pdf", "_page_" + (i+1) + "_" + suffix + ".pdf"),

                              });

                        };

                    • 8. Re: Read character 3+4 from filename and move to other position
                      jessevic Level 1

                      "The original file is open, should Acrobat resave the file?"

                       

                      I have to click on "no", but the batch is then interrupted.

                      Normally no problem if I process language file per language file, but if I have a lot of different language files per product...

                      it is only fine-tuning of the script you suggest and is already very helpful for me.

                      • 9. Re: Read character 3+4 from filename and move to other position
                        try67 MVP & Adobe Community Professional

                        This is not related to the script, it has to do with the Output settings of the Action...

                        • 10. Re: Read character 3+4 from filename and move to other position
                          jessevic Level 1

                          It seems indeed that this is the cause. At work, it went wrong. At home, the script works.

                          Tomorrow I will recheck everything again.

                          Many thanks and I keep you informed