Subject Re: manipulating a string
From Charlie <tm@tc.com>
Date Tue, 05 Apr 2022 15:09:26 -0400
Newsgroups dbase.getting-started

As always thanks very much!  This should do the trick.  Now if I can only muster up the nerve to do it on the main computer!


Mervyn Bick Wrote:

> On 2022/03/24 00:08, Charlie wrote:
> > Hi Mervyn
> >
> > Think I am missing something.  I copied the code below (both the program and function together in one program).  I changed the folder also.  But nothing really happens when I try to run it....  This is regarding the jpg folder.   The other script works well.
> >
>
> The original update_sku() will not work properly with filenames which
> include a path.  It will also update sku's for all manufacturers where
> there are 4 digits in the sku.  As I understand it, you changed only
> LIONEL sku's by filtering the table.
>
> To update filenames it needs a more complex regexp pattern.
>
> The attached program will only change the name of files where the name
> begins with the two characters LI, where there are 4 digits followed by
> at least 1 character.   If you will accept no following characters or
> must have a specific number of following characters the pattern will
> need to be changed.
>
> I've added comments to show what each part of the regexp pattern does in
> the attached program.
>
> You will need to edit the path in the program.  As it stands it will
> only recognise image files that begin with LI.  If this is not what you
> want you will need to change the following line to suit.
>
> nFiles = aFiles.dir(cFolder+'li*.jpg')
>
> Mervyn.
>
> clear
> cFolder = 'D:\examples\lionel\jpgs\'  //Include final \
> aFiles = new Array()
> nFiles = aFiles.dir(cFolder+'li*.jpg')
> f = new file()
> nCount = nFiles
> for nFile = 1 to nFiles
>   if cFolder+aFiles[ nFile,1] # update_sku1(cFolder+aFiles[nFile,1])
>       nCount --
>       ? 'Changed '+cFolder+aFiles[ nFile,1],' to  ',update_sku1(cFolder+aFiles[nFile,1])
>       f.rename(cFolder+aFiles[ nFile,1],update_sku1(cFolder+aFiles[nFile,1]))
>    endif
> next
> If nCount = nFiles
>    msgbox('No files were renamed','Done')
> endif
>
>
> function update_sku1(cString)
>    oRegExp = new OleAutoClient("VBScript.RegExp")
>    oRegExp.global  := true
>    oRegExp.ignoreCase := true
>    oRegExp.Pattern := "(\D+\\LI\d)(\d{3})([^\.|\d]\D+)"
>    // (\D+\\LI\d])   Everything up to  LI  in filename. Only accept 2 characters + 1 digit
>    // (\d{3})   Next 3 digits after LI+digit   If  only 3 digits in name it will fail.  If  5 digits the next test will fail
>    // ([^\.|\d]\D+)   [^\.|\d]  The first character after the 4 digits must not be  . or a digit
>    //                        \D+   Everything after the 4 digits must be characters.
>    a = oRegExp.execute( cString )
>    // This creates an array of matches for the regexp in each set of brackets.
>    cNew = ''
>    if oRegExp.test(cString)  //   D:\examples\lionel\jpg\li1234ex.jpg
>     // If all 3 sets of brackets return something we can proceed.
>     // If one or more set of brackes does not return a string the test fails
>     // and cNew is empy.
>       cNew = a.item(0).submatches.item(0)    //   D:\examples\lionel\jpg\li1
>       cNew=stuff(cNew,len(cNew),0,'0')       //    D:\examples\lionel\jpg\li01
>       cNew+= a.item(0).submatches.item(1)  // 234  
>       cNew+= a.item(0).submatches.item(2) //  ex.jpg
>    else
>       ?'Name not changed  ',cstring
>    endif      
>    return cNew   //D:\examples\lionel\jpg\li01234ex.jpg