Tapatalk

How to save active file with an incremented or appended number using a script?

How to save active file with an incremented or appended number using a script?

691
Advanced UserAdvanced User
691

    Jun 22, 2017#1

    I have file names of the form: FIlename_XXX.log where XXX is a version number.
    I want to to have a script which when invoked will save the file with a new filename by incrementing the version number.

    For example file Filename_003.log would be saved as Filename_004.log.
    If the filename have no 3 digit version then the script would add one starting at 000.

    Would this be possible with an UE script?

    Many thanks for any reply.

    115
    Power UserPower User
    115

      Jun 22, 2017#2

      Is the original file left unchanged or is it renamed using the new sequence number? If File_003.log is loaded and you choose run the script twice, do you get File_004.log and File_005.log or is File_004.log updated with the second execution? If you have File_005.log in UE but File_006.log already exists, does the script create File_007.log or overwrite File_006.log? What happens when you get to File_999.log?

      691
      Advanced UserAdvanced User
      691

        Jun 22, 2017#3

        @MickRC3 fair question :)

        The original file is indeed left unchanged.
        So after File_099 is saved I will have 098 other files in the folder (assuming I started from 001).
        If File_003.log is loaded and I choose run the script twice, I do indeed get File_004.log and File_005.log.
        If I have File_005.log in UE but File_006.log already exists I would be happy for the script to fail inelegantly at this point. In practice I would have a folder with DocumentX_001 and no other files so this would not happen.
        What happens when you get to File_999.log - I have not thought that far. For my purposes right now this is academic :)

        In principle is this feasible?

        I see a saveAs scripting command in help of UltraEdit on page about scripting commands. So I am thinking it may be possible. It looks like this would require extracting the current filename into a string from property path.
        Would it be true to say that this command would return the file name as well as the folder path?

        Appreciate your interest :)

        6,685587
        Grand MasterGrand Master
        6,685587

          Jun 23, 2017#4

          It is of course possible to save current file with an automatic incremented number.

          Code: Select all

          if (UltraEdit.document.length > 0)  // Is any file opened?
          {
             // Is the file not a new, unnamed file?
             if (!UltraEdit.activeDocument.isName(""))
             {
                var sFileNameWithPath = UltraEdit.activeDocument.path;
                var sFileNumber = sFileNameWithPath.replace(/^.+_(\d\d\d)\.\w+$/,"$1");
                // Does the file name end with an underscore and a number with 3 digits?
                if(sFileNumber != sFileNameWithPath)
                {
                   // Convert file number to string using decimal system and increment by 1.
                   var nFileNumber = parseInt(sFileNumber,10) + 1;
                   // Convert the incremented file number back to string using
                   // decimal system and append this string after two leading zeros.
                   var sNewFileNumber = "00" + nFileNumber.toString(10);
                   // New file number string should be just the last 3 digits.
                   sNewFileNumber = sNewFileNumber.substr(-3);
                   sFileNameWithPath = sFileNameWithPath.replace(/\d\d\d(\.\w+)$/,sNewFileNumber+"$1");
                }
                else
                {
                   // Insert _000 before file extension.
                   sFileNameWithPath = sFileNameWithPath.replace(/(\.\w+)$/,"_000$1");
                }
                UltraEdit.saveAs(sFileNameWithPath);
             }
             else
             {
                UltraEdit.messageBox("Error: Saving file with incremented number is not possible for a new file!");
             }
          }
          
          Next file number after 999 is 000 (last 3 digits of 1000) overwriting silently the file with number 000 in file name.

          By the way: Do you know that UltraEdit has a version backup feature? See Advanced - Settings/Configuration - File Handling - Backup with button ? explaining the options to define the version back format string and click on button Help for more details.
          Best regards from an UC/UE/UES for Windows user from Austria

          691
          Advanced UserAdvanced User
          691

            Jun 23, 2017#5

            Mofi ... thank you ... as ever.