23485Re: Would like better way to increment filename by one
- Dec 4, 2012--- In email@example.com, "flo.gehrke" <flo.gehrke@...> 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.
> --- In firstname.lastname@example.org, "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
> ^!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%)$
> ^!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... ;-)
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.
- << Previous post in topic Next post in topic >>