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

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

Expand Messages
  • 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 1 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 2 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 3 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 4 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.