Subject Re: manipulating a string
From Mervyn Bick <invalid@invalid.invalid>
Date Thu, 24 Mar 2022 12:19:57 +0200
Newsgroups dbase.getting-started
Attachment(s) update_sku_jpg.prg

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