• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

Find Replace

I have a folder of .xml files that I need to run a find replace script against the contents, that will find a string of characters based on, if the same character is repeated in a string 4 times or more.

For example:
if the string looks like this   RRRR then replace with &&&&
if the string looks like this   RRRRRRRR then replace with &&&&&&&&
if the string looked like this RRR then skip

How can this be done?
Many thanks in advance
0
H-SC
Asked:
H-SC
  • 5
  • 5
1 Solution
 
Bill PrewCommented:
I it just R repeats you are looking for, or any characters?

If other characters than R, what would those be replaced with, & as well?

~bp
0
 
H-SCAuthor Commented:
only the repeating => 4 "R" both caps or lowercse. gets replaced with "&"
0
 
Bill PrewCommented:
Okay, that feels pretty doable. What's the maximum number of R's repeated that can ever occur?

~bp
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
H-SCAuthor Commented:
hmmm, maybe 50
0
 
H-SCAuthor Commented:
?
0
 
Bill PrewCommented:
Was traveling last week, will take a look at this today or tomorrow and put together an approach.

~bp
0
 
H-SCAuthor Commented:
Not a problem at all, no rush,
..I knew that I may start getting the clean up outstanding question emails soon. and wanted to follow up
thanks in advance for any help that you can provide
0
 
Bill PrewCommented:
Okay, see if this does the replacements properly on a couple of test files.  \Save as a VBS and run like this:

cscript EE27029715.vbs EE27029715.in EE27029715.out

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
  sInfile = WScript.Arguments(0)
Else
  WScript.Echo "No filename specified."
  WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
  sOutfile = WScript.Arguments(1)
Else
  sOutfile = sInfile
End If

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Read entire input file into a variable and close it
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
sData = oInfile.ReadAll
oInfile.Close
Set oInfile = Nothing

' Do replace logic
Set oRegExp = New RegExp
oRegExp.Global = True
oRegExp.IgnoreCase = False
oRegExp.Pattern = "(RRRR+)"
Set oFound = oRegExp.Execute(sData)

' Process each match and replace it
for Each oMatch in oFound
   sData = Left(sData, oMatch.FirstIndex - 1) & Replace(sData, oMatch.Value, String(oMatch.Length, "&"), oMatch.FirstIndex, 1)
Next

' Write file with any changes made
Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)
oOutfile.Write(sData)
oOutfile.Close
Set oOutfile = Nothing

' Cleanup and end
Set oFSO = Nothing
Wscript.Quit

Open in new window

~bp
0
 
H-SCAuthor Commented:
Bill,

This is genius!!  Works great.
Many thanks for your help with this.
0
 
Bill PrewCommented:
Welcome, glad that helped.

~bp
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now