Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search (wild card) and replace string in text file

Status
Not open for further replies.

aruddyk

MIS
Nov 14, 2008
2
0
0
SG
I have some text files which contain the following entries:
names=names.nsf,names1.nsf,names2.nsf
preferences=1245636456
Ports=TCPIP,LAN0,COM1,COM2
DisabledPorts=COM3,COM4,COM5
names=names.nsf
preferences=3185
Ports=TCPIP
DisabledPorts=LAN0,COM1,COM2,COM3,COM4,COM5
The first 4 entries are garbage entries, I do not want them. The last 4 are the correct one.
How do I get rid the first 4 entries, assuming each file have different entry.
Below is the sample code created but I couldn't make it work. Thank you.
.......................................................
on error resume next
Const ForReading = 1
Const ForWriting = 2

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "DisabledPorts"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\NOTESBKP\Fail\Text.txt", ForReading)

Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
Set colMatches = objRegEx.Execute(strSearchString)
If colMatches.Count > 0 Then
For Each strMatch in colMatches
If strSearchString = "DisabledPorts=LAN0,COM1,COM2,COM3,COM4,COM5" Then
MsgBox "Don't do anything : " & strSearchString
Else
MsgBox "Delete this string: " & strSearchString
objFile1.Write(Replace(strSearchString, strSearchString,"DisabledPorts=LAN0,COM1,COM2,COM3,COM4,COM5"))
End If
Next
End If
Loop
 

Trying to get the scenario straight.

This is your (whole) text file:
Code:
[red]names=names.nsf,names1.nsf,names2.nsf
preferences=1245636456
Ports=TCPIP,LAN0,COM1,COM2
DisabledPorts=COM3,COM4,COM5[/red]
[blue]names=names.nsf
preferences=3185
Ports=TCPIP
DisabledPorts=LAN0,COM1,COM2,COM3,COM4,COM5[/blue]
and you ALWAYS want to get rid of RED lines (first 4) and ALWAYS leave the BLUE lines (last 4)?

If that's the case, why not just use regular OPEN statement, by-pass first 4 lines, read last 4 lines and write them into another text file. Pretty simple task, no FSO required.

Have fun.

---- Andy
 
Those entries are not always in sequnce order, meaning those in RED or BLUE entries can be at the bottom or middle or top position, they are scattered everywhere.
My sample code is trying to get rid of "DisabledPorts" entry, I have yet to figure out for the rest. Thanks.
 
Hi Aruddyk,

how about this solution:
Code:
Sub CleanupFile()
Dim fso As FileSystemObject, f As File, TsIn As TextStream, TsOut As TextStream
Dim buf As String
Dim nams As String, prefs As String, ports As String, disab As String

Set fso = New FileSystemObject
Set f = fso.GetFile("C:\NOTESBKP\Fail\Text.txt")
Set TsIn = f.OpenAsTextStream(ForReading)
Set TsOut = fso.CreateTextFile("C:\tmp.txt", True)

'Loop through input file; if second instance of "names", simply overwrite with latest value
Do While Not TsIn.AtEndOfStream
    buf = TsIn.ReadLine
    Select Case LCase(Left(buf, 4))
        Case "name"
            nams = buf
        Case "pref"
            prefs = buf
        Case "port"
            ports = buf
        Case "disa"
            disab = buf
    End Select
Loop
TsIn.Close
'*****************
'***Write latest values as stored in variables into temp output file
TsOut.WriteLine nams
TsOut.WriteLine prefs
TsOut.WriteLine ports
TsOut.WriteLine disab

TsOut.Close
'****************
'*Delete old source file, overwrite with newly created file
Kill "C:\NOTESBKP\Fail\Text.txt"
fso.MoveFile "C:\tmp.txt", "C:\NOTESBKP\Fail\Text.txt"
'****************
'Destroy objects
Set f = Nothing
Set TsIn = Nothing
Set TsOut = Nothing
Set fso = Nothing

End Sub[/quote]

This will create a file containing the respective last entries of "names, ports" etc...

Would that do the trick?

If you want to remove the "disabled" then just don't assign any value to the output variable...
;-)

Cheers,
MiS

[b][navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell[/b]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top