Loading ...
Sorry, an error occurred while loading the content.

RE: [Clip] Would like better way to increment filename by one

Expand Messages
  • John Shotsky
    If I were doing it, I wouldn t start with leading zeros and then fight them. I d choose an actual number, such as 10000 as my zero and increment that.
    Message 1 of 13 , Dec 4, 2012
    • 0 Attachment
      If I were doing it, I wouldn't start with leading zeros and then fight them. I'd choose an actual number, such as 10000
      as my 'zero' and increment that.

      Regards,
      John
      RecipeTools Web Site: <http://recipetools.gotdns.com/> http://recipetools.gotdns.com/

      From: ntb-clips@yahoogroups.com [mailto:ntb-clips@yahoogroups.com] On Behalf Of joy8388608
      Sent: Monday, December 03, 2012 15:52
      To: ntb-Clips@yahoogroups.com
      Subject: [Clip] Would like better way to increment filename by one


      I thought this would be easier than it was and can't shake the feeling that there is a better way than what I can come
      up with so I ask for creative ideas here.

      I wanted a subroutine to increment trailing digits of filename. This is useful for creating backups and sequential
      files.

      Parsing the filename itself is no problem (i.e., picking off the folder, drive, etc. to isolate the actual file name).
      What I want is to increment the trailing digits while leaving AT LEAST the same number of digits as examples below.

      bkup3->bkup4 bkup01->bkup02 bkup99->bkup100 bkup0098->bkup0099->bkup0100

      If no numeric trailing digits, append 01. bkup->bkup01

      This is all relatively simple except for the keeping (at least) the same number of digits since ^!Inc strips leading
      zeros and adding one to 0098 would give 99.

      I created the routine by isolating the trailing digits, adding one and padding the result to the proper length.

      Can anyone come up with an elegant way to convert bkup009 to bkup010 and bkup999 to bkup1000?

      (This would be a nice built-in function, eh?)

      Thanks,
      Joy



      [Non-text portions of this message have been removed]
    • flo.gehrke
      ... If I m not mistaken this should match your data and expected results: ; Long line start ^!SetArray
      Message 2 of 13 , Dec 4, 2012
      • 0 Attachment
        --- In ntb-clips@yahoogroups.com, "joy8388608" <mycroftj@...> wrote:
        >
        > What I want is to increment the trailing digits while leaving
        > AT LEAST the same number of digits as examples below.
        >
        > bkup3->bkup4 bkup01->bkup02 bkup99->bkup100 bkup0098->bkup0099->bkup0100
        >
        > If no numeric trailing digits, append 01. bkup->bkup01
        > (...)
        > Can anyone come up with an elegant way to convert bkup009 to bkup010 and bkup999 to bkup1000?

        If I'm not mistaken this should match your data and expected results:

        ; Long line start
        ^!SetArray %List%=bkup;bkup3;bkup9;bkup01;bkup98;bkup99;bkup0098;bkup0099;bkup0998;bkup0999;bkup1000;bkup9998
        ; Long line end
        ^!Set %i%=1
        :Loop
        ^!Set %FileName%=^%List^%i%%
        ^!IfSame "bkup" "^%FileName%" Next Else Skip_2
        ^!Set %Nr%=01
        ^!Goto Insert
        ^!Set %Nr%=^$StrReplace("[^\d]";"";"^%FileName%";RA)$
        ^!Set %Size%=^$StrSize(^%Nr%)$
        ^!IfMatch "bkup99|bkup9" "^%FileName%" ^!Inc %Size%
        ^!Inc %Nr%
        ^!Set %Nr%=^$StrCopyRight("000^%Nr%";^%Size%)$
        :Insert
        ^!InsertText Next backup for ^%FileName% is bkup^%Nr%^%NL%
        ^!Inc %i%
        ^!If ^%i% > ^%List0% End
        ^!Goto Loop

        I'll leave it open whether it's "elegant" or not... ;-)

        Regards,
        Flo
      • Eb
        Joy, John s excellent suggestion eliminates a lot of code, to left-pad the version number. Separating the version number from the name makes isolating the
        Message 3 of 13 , Dec 4, 2012
        • 0 Attachment
          Joy,

          John's excellent suggestion eliminates a lot of code, to left-pad the version number. Separating the version number from the name makes isolating the number easier.

          I leave it to you to generate the original filename. A couple of tips though:

          Use something like the following to create a permanent default numbering system:

          ^!Set %width%=^?{enter the width of your version numbers}
          ^!IfFalse ^$IsNumber(^%width%)$ SKIP_-1
          ^!SaveValue FileVer:Width=^%width%
          ;
          ^!Dec %width%
          ^!Set %ver%=1^$StrFill("0";^%width%)$

          Use an underscore between the filename and the version number, like "filename_1000.txt". This will let you extract the version number as an array element. See below.


          Incrementing the file version of the current document,
          like "c:\path\file_10000.txt":

          H="IncDocVersion"
          :UNTESTED
          ;separate the filespec into 4 parts:
          ; path + file + version + extension
          ^!Set %path%=^$GetPath(^**)$
          ^!Set %fname%=^$GetName(^**)$
          ^!Set %ext%=^$GetExt(^**)$
          ;
          ^!SetListDelimiter _
          ^!SetArray %cname%=^%fname%
          ;Error trap for files w/o "_nnn"
          ^!If ^%cname0%<2 NO_VERNUM
          ;
          ;Increment the version number
          ^!Inc %cname2%
          ;
          :SAVE_NEW_NAME
          ;Reassemble the name
          ^!Save AS "^%path%^%cname%^%ext%"
          ^!Goto END
          :NO_VERNUM
          ^!Set %width%=^$GetValue(Value FileVer:Width)$
          ^!Dec %width%
          ^!Set %ver%=1^$StrFill("0";^%width%)$
          ^!SetArray %cname%=^%cname%_^%ver%
          ^!Goto SAVE_NEW_NAME

          If the underscore interferes with your naming scheme, use a different character, that is unique in the name, AND is a legal character for filenames.


          Cheers,


          Eb

          --- In ntb-clips@yahoogroups.com, "John Shotsky" <jshotsky@...> wrote:
          >
          > If I were doing it, I wouldn't start with leading zeros and then fight them. I'd choose an actual number, such as 10000
          > as my 'zero' and increment that.
          > ...
          > From: ntb-clips@yahoogroups.com [mailto:ntb-clips@yahoogroups.com] On Behalf Of joy8388608
          >
          > ... ask for creative ideas here.
          >
          > I wanted a subroutine to increment trailing digits of filename. This is useful for creating backups and sequential
          > files.
          ...

          > bkup3->bkup4 bkup01->bkup02 bkup99->bkup100 bkup0098->bkup0099->bkup0100
          >
          > If no numeric trailing digits, append 01. bkup->bkup01
          >
          > This is all relatively simple except for the keeping (at least) the same number of digits since ^!Inc strips leading
          > zeros and adding one to 0098 would give 99.
          >
          > I created the routine by isolating the trailing digits, adding one and padding the result to the proper length.
          >
          > Can anyone come up with an elegant way to convert bkup009 to bkup010 and bkup999 to bkup1000?
          >
          > (This would be a nice built-in function, eh?)
          >
          > Thanks,
          > Joy
          >
          >
          >
          > [Non-text portions of this message have been removed]
          >
        • joy8388608
          ... Very interesting thoughts from everyone and I m kind of happy that there have been no why didn t *I* think of that answers so far. To clarify some
          Message 4 of 13 , Dec 4, 2012
          • 0 Attachment
            --- In ntb-clips@yahoogroups.com, "flo.gehrke" <flo.gehrke@...> wrote:
            >
            > --- In ntb-clips@yahoogroups.com, "joy8388608" <mycroftj@> wrote:
            > >
            > > What I want is to increment the trailing digits while leaving
            > > AT LEAST the same number of digits as examples below.
            > >
            > > bkup3->bkup4 bkup01->bkup02 bkup99->bkup100 bkup0098->bkup0099->bkup0100
            > >
            > > If no numeric trailing digits, append 01. bkup->bkup01
            > > (...)
            > > Can anyone come up with an elegant way to convert bkup009 to bkup010 and bkup999 to bkup1000?
            >
            > If I'm not mistaken this should match your data and expected results:
            >
            > ; Long line start
            > ^!SetArray %List%=bkup;bkup3;bkup9;bkup01;bkup98;bkup99;bkup0098;bkup0099;bkup0998;bkup0999;bkup1000;bkup9998
            > ; Long line end
            > ^!Set %i%=1
            > :Loop
            > ^!Set %FileName%=^%List^%i%%
            > ^!IfSame "bkup" "^%FileName%" Next Else Skip_2
            > ^!Set %Nr%=01
            > ^!Goto Insert
            > ^!Set %Nr%=^$StrReplace("[^\d]";"";"^%FileName%";RA)$
            > ^!Set %Size%=^$StrSize(^%Nr%)$
            > ^!IfMatch "bkup99|bkup9" "^%FileName%" ^!Inc %Size%
            > ^!Inc %Nr%
            > ^!Set %Nr%=^$StrCopyRight("000^%Nr%";^%Size%)$
            > :Insert
            > ^!InsertText Next backup for ^%FileName% is bkup^%Nr%^%NL%
            > ^!Inc %i%
            > ^!If ^%i% > ^%List0% End
            > ^!Goto Loop
            >
            > I'll leave it open whether it's "elegant" or not... ;-)
            >
            > Regards,
            > Flo
            >


            Very interesting thoughts from everyone and I'm kind of happy that there have been no "why didn't *I* think of that" answers so far.

            To clarify some things...

            "bkup" was just an example. I want a routine that works with any and all file names.
            For this same reason, starting numbering with 10000 and using an underscore to make the number easier to find is not an option.

            I do very much agree with Axel in that clear, plodding code that can be easily understood is usually better than shorter, "clever" code that is difficult to follow. That and lots of comments! Anyone who has had to modify other people's code know what I'm talking about.

            Other languages have numeric formatting and special commands that would make this easy but NT does not. So, for now, I think what I have is the best I've come across unless someone comes up with an idea. My code is not available to me at this moment but I'd be happy to post what I have later if anyone has an interest. Just ask.

            I'll be still thinking about this one.
            Thanks!
            Joy
          • Ian NTnerd
            ... Sorry my last post had junk in it. Let me know what you think. I d like to see what you have now. Ian H= _incfile ^!SetDebug Off ; Get the file name (no
            Message 5 of 13 , Dec 4, 2012
            • 0 Attachment
              On 5/12/2012 6:46 AM, joy8388608 wrote:
              >
              >
              > Very interesting thoughts from everyone and I'm kind of happy that
              > there have been no "why didn't *I* think of that" answers so far.
              >
              > To clarify some things...
              >
              > "bkup" was just an example. I want a routine that works with any and
              > all file names.
              > For this same reason, starting numbering with 10000 and using an
              > underscore to make the number easier to find is not an option.
              >
              > I do very much agree with Axel in that clear, plodding code that can
              > be easily understood is usually better than shorter, "clever" code
              > that is difficult to follow. That and lots of comments! Anyone who has
              > had to modify other people's code know what I'm talking about.
              >
              > Other languages have numeric formatting and special commands that
              > would make this easy but NT does not. So, for now, I think what I have
              > is the best I've come across unless someone comes up with an idea. My
              > code is not available to me at this moment but I'd be happy to post
              > what I have later if anyone has an interest. Just ask.
              >
              > I'll be still thinking about this one.
              > Thanks!
              > Joy
              >
              >
              Sorry my last post had junk in it. Let me know what you think. I'd like
              to see what you have now.

              Ian

              H="_incfile"
              ^!SetDebug Off
              ; Get the file name (no path or extension)
              ^!set %input%=^&
              ; start loop from ClipHelp.clb
              ^!Set %Index%=0
              ^!Set %LoopCount%=10
              ^!IfCancel End
              :LoopStart
              ^!Inc %Index%
              ^!If ^%Index% > ^%LoopCount% EndLoop
              ; test if the string that is ^%index% chars long is a number if not end loop
              ^!if ^$IsNumber(^$StrCopyRight("^%input%";^%index%)$)$=0 endloop
              ; create a string of zeros to be longer than needed
              ^!set %zeros%=^%zeros%0
              ; return to ClipHelp.clb closing section
              ^!GoTo LoopStart
              :EndLoop

              ; test if the ^%index% is 1, if it is then there is no number at the
              end. Do nonumber else do has number.
              ^!if ^%index%=1 nonumber else hasnumber
              :nonumber
              ; if no number create the string to return
              ^!set %return%=^%input%01
              ^!goto return
              :hasnumber
              ; if number found, decrease the index to the correct figure.
              ^!dec %index%
              ; get the non number part of the string
              ^!set %alpha%=^$StrDeleteRight("^%input%";^%index%)$
              ; get the number part and increase it by one
              ^!set %numb%=^$StrCopyRight("^%input%";^%index%)$
              ^!inc %numb%
              ; set the return value with alpha part and new number trimed to the
              input length.
              ; this will fail if your input string is 99 and it is increased to 100
              ^!set %return%=^%alpha%^$StrCopyRight("^%zeros%^%numb%";^%index%)$
              :return
              ^!result ^%return%

              H="TestInc"
              ^$incfile(^?[Word to use=word0101010108])$



              [Non-text portions of this message have been removed]
            • joy8388608
              ... Sure. Sorry for the delay. I was away. I usually use farclips instead of functions since I can call them from any library. The calling clip sets a variable
              Message 6 of 13 , Dec 6, 2012
              • 0 Attachment
                --- In ntb-clips@yahoogroups.com, Ian NTnerd <indiamcq@...> wrote:
                >
                > On 5/12/2012 6:46 AM, joy8388608 wrote:
                > >
                > >
                > > Very interesting thoughts from everyone and I'm kind of happy that
                > > there have been no "why didn't *I* think of that" answers so far.
                > >
                > > To clarify some things...
                > >
                > > "bkup" was just an example. I want a routine that works with any and
                > > all file names.
                > > For this same reason, starting numbering with 10000 and using an
                > > underscore to make the number easier to find is not an option.
                > >
                > > I do very much agree with Axel in that clear, plodding code that can
                > > be easily understood is usually better than shorter, "clever" code
                > > that is difficult to follow. That and lots of comments! Anyone who has
                > > had to modify other people's code know what I'm talking about.
                > >
                > > Other languages have numeric formatting and special commands that
                > > would make this easy but NT does not. So, for now, I think what I have
                > > is the best I've come across unless someone comes up with an idea. My
                > > code is not available to me at this moment but I'd be happy to post
                > > what I have later if anyone has an interest. Just ask.
                > >
                > > I'll be still thinking about this one.
                > > Thanks!
                > > Joy
                > >
                > >
                > Sorry my last post had junk in it. Let me know what you think. I'd like
                > to see what you have now.
                >
                > Ian


                Sure. Sorry for the delay. I was away.

                I usually use farclips instead of functions since I can call them from any library. The calling clip sets a variable and the result(s) are passed back in another variable.

                Now that everyone sees what I came up with, I'm sure someone will improve upon it!

                Joy

                Increment_FileName
                ; Increment number at end of passed file name
                ; IN: %Incr_FN_I% is filename with optional drive and path and extension
                ;
                ; OUT: %Incr_FN_O% is new filename with added or incremented number
                ;
                ; Increments trailing digits of filename.
                ; If no numeric trailing digits, append 01.
                ; Incremented number is at least as long as original number.
                ; 3->4 01->02 99->100 0098->0099->0100

                ; call with ^!FarClip "<LibraryName>:Increment_FileName"
                ; Handles trailing numeric value up to 9223372036854775806

                ; Get just the File Name without path or extension
                ^!Set %INFN_JUSTFN%=^$GetName(^%Incr_FN_I%)$

                ; Find start position of trailing digits
                ^!Set %INFN_StartPos_Trailing_Digits%=^$StrPos("\d+$";"^%INFN_JUSTFN%";R)$

                ^!If ^%INFN_StartPos_Trailing_Digits% = 0 NO_NUMBERS

                ; Isolate the trailing numbers
                ^!Set %INFN_TrailingValue%=^$StrCopy("^%INFN_JUSTFN%";^%INFN_StartPos_Trailing_Digits%;100)$

                ; Find number of digits at the end of filename
                ^!Set %INFN_Num_of_Orig_Digits%=^$StrSize("^%INFN_TrailingValue%")$

                ; Remove trailing numeric chars from the filename
                ^!Set %INFN_FN_WITHOUT_TrailingNums%=^$StrCopyLeft("^%INFN_JUSTFN%";^$Calc(^$StrSize("^%INFN_JUSTFN%")$ - ^%INFN_Num_of_Orig_Digits%)$)$

                ; Add one to trailing number
                ^!Inc %INFN_TrailingValue%

                ; If size original GT size new, add correct number of preceding zeros
                ; If same size or smaller, leave var empty
                ^!Set %INFN_SizeDiff%=^$Calc(^%INFN_Num_of_Orig_Digits% - ^$StrSize("^%INFN_TrailingValue%")$)$

                ^!If ^%INFN_SizeDiff% <= 0 SKIP_1
                ^!Set %INFN_Zeros%=^$StrFill("0";^%INFN_SizeDiff%)$

                ; Create just the filename with the formatted incremented number
                ^!Set %INFN_NEWFN%=^%INFN_FN_WITHOUT_TrailingNums%^%INFN_Zeros%^%INFN_TrailingValue%

                ; If they were given, put back the path and extention
                ^!Set %Incr_FN_O%=^$GetPath(^%Incr_FN_I%)$^%INFN_NEWFN%^$GetExt(^%Incr_FN_I%)$
                ^!Goto INFN_CLEAR

                :NO_NUMBERS
                ; No trailing digits found so just append 01
                ^!Set %Incr_FN_O%=^$GetPath(^%Incr_FN_I%)$^%INFN_JUSTFN%01^$GetExt(^%Incr_FN_I%)$

                :INFN_CLEAR
                ^!ClearVariable %INFN_Zeros%
                ^!ClearVariable %INFN_NEWFN%
                ^!ClearVariable %INFN_JUSTFN%
                ^!ClearVariable %INFN_SizeDiff%
                ^!ClearVariable %INFN_TrailingValue%
                ^!ClearVariable %INFN_Num_of_Orig_Digits%
                ^!ClearVariable %INFN_StartPos_Trailing_Digits%
                ^!ClearVariable %INFN_FN_WITHOUT_TrailingNums%
              • Ian NTnerd
                ... Joy Your clip gots rid of the Loop I used with this RegEx. So it is faster. ^!Set %INFN_StartPos_Trailing_Digits%=^$StrPos( d+$ ; ^%INFN_JUSTFN% ;R)$ I
                Message 7 of 13 , Dec 6, 2012
                • 0 Attachment
                  On 7/12/2012 3:58 AM, joy8388608 wrote:
                  > I usually use farclips instead of functions since I can call them from
                  > any library. The calling clip sets a variable and the result(s) are
                  > passed back in another variable.
                  Joy

                  Your clip gots rid of the Loop I used with this RegEx. So it is faster.
                  ^!Set %INFN_StartPos_Trailing_Digits%=^$StrPos("\d+$";"^%INFN_JUSTFN%";R)$

                  I like that.

                  You mentioned you use farclips rather than functions. I have sometimes
                  used a local function called farfunc, so I get the convenience of a
                  function (used inline), and the re-usability of a far clip. You have to
                  modify your far clips to give the result in the variable ^%outvalue%

                  ; FarFunc can call some functions in another clipbook
                  ; expected parameter: farlibrary:farclip;param1;param2;paramX

                  ; assign the input to a variable
                  ^!set %input%=^&

                  ; make an array of the input
                  ^!setlistdelimiter ;
                  ^!setarray %part%=^%input%

                  ; Get just the parameters by removing the farlibrary and the farclip part
                  ^!set %param%=^$StrReplace("^%part1%;";"";"^%input%";No;No)$

                  ; call the far clip
                  ^!farclip "^%part1%" ^%param%

                  ; return the result
                  ^!result ^%outvalue%

                  That may make your basic code look more elegant.

                  Ian
                • Ian NTnerd
                  ... Combining your variables with the tip from another post on padding to a length, the above could be rewritten as ^!Set
                  Message 8 of 13 , Dec 6, 2012
                  • 0 Attachment
                    On 7/12/2012 3:58 AM, joy8388608 wrote:
                    > ; If size original GT size new, add correct number of preceding zeros
                    > ; If same size or smaller, leave var empty
                    > ^!Set %INFN_SizeDiff%=^$Calc(^%INFN_Num_of_Orig_Digits% -
                    > ^$StrSize("^%INFN_TrailingValue%")$)$
                    >
                    > ^!If ^%INFN_SizeDiff% <= 0 SKIP_1
                    > ^!Set %INFN_Zeros%=^$StrFill("0";^%INFN_SizeDiff%)$
                    >
                    > ; Create just the filename with the formatted incremented number
                    > ^!Set
                    > %INFN_NEWFN%=^%INFN_FN_WITHOUT_TrailingNums%^%INFN_Zeros%^%INFN_TrailingValue%
                    >
                    > ; If they were given, put back the path and extention
                    > ^!Set
                    > %Incr_FN_O%=^$GetPath(^%Incr_FN_I%)$^%INFN_NEWFN%^$GetExt(^%Incr_FN_I%)$
                    > ^!Goto INFN_CLEAR
                    Combining your variables with the tip from another post on padding to a
                    length, the above could be rewritten as

                    ^!Set
                    %Incr_FN_O%=^$GetPath(^%Incr_FN_I%)$^$StrCopyRight("00000000000000000000^%%INFN_TrailingValue%%";^%INFN_Num_of_Orig_Digits%)$^$GetExt(^%Incr_FN_I%)$
                    ^!Goto INFN_CLEAR

                    Ian
                  Your message has been successfully submitted and would be delivered to recipients shortly.