9 Replies Latest reply on Jun 13, 2013 9:58 PM by adraft

    Incremental Numbering for File Names

    adraft

      I've been trying to wrap my head around this problem all day. So I've found two very helpful scripts on this forum: here and here. The second one works, but I'd like to be able to direct where the files are saving to and I'd like to save them as .tifs. It seems like the first script is exactly the one I want, but when I run it I get the following error (I have CS 5):

       

      12--8_2013-05-14_15-37-23.jpg

       

      I'm trying to use this as a learning exercise, but man, it's a steep curve.

       

      Thanks

       

      jonathan.

        • 1. Re: Incremental Numbering for File Names
          Michael L Hale Level 5

          From that error message I would say that you have a bad file path. The saveAs() command will not create subfolders if they do not exists. At a guess you are trying to create a new folder and save to that folder in one step. You need to first create the folder, then you can save to it.

           

          Or if the D: drive on your system is a CD/DVD drive then that may be the problem.

          • 2. Re: Incremental Numbering for File Names
            adraft Level 1

            Thank you for your response. The script only works for me when I have it save to a folder, which is fine. For some reason specifying subfolders (already existing ones) brings up that error message again.

             

            This will work for me, though. Thanks again.

            • 3. Re: Incremental Numbering for File Names
              Michael L Hale Level 5

              You should be able to save to an existing subfolder unless it's a folder that can't be written to because of user permissions.

               

              Can you post the line that defines 'saveFile'? It could be that you are not creating the path correctly. It's had to tell from the error message as some of the path looks like it was replaced with '...'

              • 4. Re: Incremental Numbering for File Names
                adraft Level 1

                var saveFile = new File( saveFolder + '/' + saveName + '_' + zeroPad( saveNumber, saveSufixLength ) + '.' + saveExt );

                 

                and for saveFolder I've got:

                 

                var saveFolder = new Folder( 'D:\Civil War\Process\Screen Caps' );

                 

                the script works when the path is D:\Civil War, but when I enter any subfolders I get the error.

                • 5. Re: Incremental Numbering for File Names
                  Michael L Hale Level 5

                  The backslash in javascript is a special char used to escape the following char. So either use

                   

                  var saveFolder = new Folder( 'D:\\Civil War\\Process\\Screen Caps' );

                   

                  Or better yet avoid the unnecessary use of the special char.

                   

                  var saveFolder = new Folder( '/D/Civil War/Process/Screen Caps' );

                  1 person found this helpful
                  • 6. Re: Incremental Numbering for File Names
                    adraft Level 1

                    huzzah! thank you!

                    • 7. Re: Incremental Numbering for File Names
                      adraft Level 1

                      Oops. For some reason after getting to file number _008, new saves are numbered _001 (and rewrite the existing _001). Any idea why this would be happening?

                      • 8. Re: Incremental Numbering for File Names
                        Michael L Hale Level 5

                        It's hard to say without seeing your code but I would guess that there is something wrong with how 'saveNumber' is being created/updated.

                         

                        You can avoid overwritting files by checking if they exists before the save step. If they don't exists it is safe to save. If they do exists then you have to modify saveFile.

                         

                        But I would fix the 'saveNumber' problem first. Fixing that may take care of overwritting.

                        • 9. Re: Incremental Numbering for File Names
                          adraft Level 1

                          I got the script working. Took out some of the superfluous steps. Here it is in case anyone is curious:

                           

                          // This script will save the active document to a folder with an incremental sufix

                          // Change the options below to match your needs

                          var saveFolder = new Folder( '/D/Civil War/Process/Screen Caps' );

                          var saveExt = 'tif';

                          var saveSufixStart = '_';

                          var saveSufixLength = 3;

                          tifOpts = new TiffSaveOptions();

                          tifOpts.embedColorProfile = true;

                          tifOpts.imageCompression = TIFFEncoding.NONE;

                          tifOpts.alphaChannels = false;

                          tifOpts.layers = false;

                          // End of user options

                          //==========================================

                          function zeroPad ( num, digit ){

                             var tmp = num.toString();

                             while (tmp.length < digit) { tmp = "0" + tmp;}

                             return tmp;

                          }

                          var docName = decodeURI ( activeDocument.name );

                          docName = docName.match( /(.*)(\.[^\.]+)/ ) ? docName = docName.match( /(.*)(\.[^\.]+)/ ) : docName = [ docName, docName, undefined ];

                          var saveName = docName[ 1 ]; // activeDocument name with out ext

                          var files = saveFolder.getFiles( saveName + '*.' + saveExt );// get an array of files matching doc name prefix

                           

                           

                          var saveNumber = files.length + 1;

                          alert("New file number: " + zeroPad( saveNumber, saveSufixLength ));

                          var saveFile = new File( saveFolder + '/' + saveName + '_' + zeroPad( saveNumber, saveSufixLength ) + '.' + saveExt );

                          activeDocument.saveAs( saveFile, tifOpts ,true ,Extension.LOWERCASE);