I have been struggling to find a way to do a regexp.replace in multiple
files, if anyone can help? My company downloads a series of files daily.
Each file is a single customer transsaction and contains a customer account
number that is in the format of the letters "A14*CA" followed by a sixteen
digit number on its own line. The customer account number needs to be adited
so that the backoffice software that imports the files can read it, but the
backoffice software only accepts a ten digit customer account number. So, I
need to replace the sixteen digit number with a ten digit number, but the
digits my company wants are characters 4-10 following the begining letters
above. For example, the customer account line may read
"A14*CA4290423634535002" and needs to be replaced with "A14*CA0423634535"
and the file needs to be saved. This customer account information is on
it's own line but the line number may vary. All customer transaction files
are in the same folder.

-David

Re: VBScript Regexp Customer Account by James

James
Tue Apr 29 09:06:18 CDT 2008

"David" <dwilliams@aaamv.com> wrote in message
news:O31%23NOfqIHA.5416@TK2MSFTNGP06.phx.gbl...
>I have been struggling to find a way to do a regexp.replace in multiple
>files, if anyone can help? My company downloads a series of files daily.
>Each file is a single customer transsaction and contains a customer account
>number that is in the format of the letters "A14*CA" followed by a sixteen
>digit number on its own line. The customer account number needs to be
>adited so that the backoffice software that imports the files can read it,
>but the backoffice software only accepts a ten digit customer account
>number. So, I need to replace the sixteen digit number with a ten digit
>number, but the digits my company wants are characters 4-10 following the
>begining letters above. For example, the customer account line may read
>"A14*CA4290423634535002" and needs to be replaced with "A14*CA0423634535"
>and the file needs to be saved. This customer account information is on
>it's own line but the line number may vary. All customer transaction files
>are in the same folder.

Just as a hard-coded example using 'File.txt', see if the below code
performs the replacement as requested.

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oRegEx = CreateObject("VBScript.RegExp")

sFile = "File.txt"

oRegEx.Pattern = "^(A14\*CA)\d{3}(\d{10})\d{3}$"
oRegEx.Multiline = True

Set oFile = oFSO.OpenTextFile(sFile)
sText = oFile.ReadAll
oFile.Close

If oRegEx.Test(sText) Then
Set oFile = oFSO.OpenTextFile(sFile, 2, True)
oFile.Write oRegEx.Replace(sText, "$1$2")
oFile.Close
End If
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The pattern will look for a line beginning with 'A14*CA' and match it,
followed by 3 digits (not matched), followed by 10 digits (matched),
followed by 3 digits (not matched) followed by the end of the line. The
matched line with be replaced by the first and second submatches.

If the above works, you just need to add code to enumerate the directory
with your files.