I am attempting to replace a string of text which includes a number with new text which also includes new numbers. Such as:
Old strings
"NUM COLUMNS=21659"
"NUM ROWS=16244"
and replace those with:
"NUM COLUMNS=10831"
"NUM ROWS=8122"
The issue is the numbers on the Old strings can vary. The numbers are part of the string and not considered integers. The new numbers are constant so that is not an issue. Here is what I have so far but not complete as it does not include the old numbers:
I have been reading up on using a pattern to capture any numbers after "NUM COLUMNS=" and "NUM ROWS=" using something like:Code:Option Explicit Sub EditGCP() Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject") Dim sFolder : sFolder = "P:\Temp" Dim OldSizeColumns : OldSizeColumns = "NUM COLUMNS=" Dim OldSizeRows : OldSizeRows = "NUM ROWS=" Dim OldSizeColumns : NewSizeColumns = "NUM COLUMNS=10831" Dim OldSizeRows : NewSizeRows = "NUM ROWS=8124" Dim inFile, tempFile, strText, strNewText, strNewText1 Const ForReading = 1 Const ForWriting = 2 Const OverwriteExisting = TRUE For Each infile In objFSO.GetFolder(sFolder).Files If (objFSO.getExtensionName(inFile.path))="txt" Then Set tempFile = objFSO.OpenTextFile(infile, 1) strText = tempFile.ReadAll tempFile.Close strNewText = Replace(strText, OldSizeColumns, NewSizeColumns) strNewText1 = Replace(strNewText, OldSizeRows, NewSizeRows) Set tempFile = objFSO.OpenTextFile(Infile, 2) tempFile.Write strNewText1 tempFile.Close Else End If Next End Sub
Dim r
Set r = New RegExp
r.Pattern="NUM COLUMNS=\(\d+\)"
or
Dim r
Set r = New RegExp
r.Pattern="NUM COLUMNS=\[0-9]+"
and
strNewText = r.Replace(OldSizeColumns, NewSizeColumns)
However when I apply them it deletes all text in the file and just leaves "NUM COLUMNS="!
I thought maybe one could use a 'replace line' command of some type since "NUM COLUMNS=" is always on line 24 and "NUM ROWS=" is always on line 25 but could not find enough information yet to make an attempt.
Any help would be most appreciated.




Reply With Quote
