Link to home
Start Free TrialLog in
Avatar of bt707
bt707Flag for United States of America

asked on

vbs - replace line in file

I need to replace a line in a file that could be found several times, not sure how to use a reg expression to do what I need, I get part of it replaced but not coming out how I need it.

For example, in a file lines found that look like:

PopServer=server1.mail.xyz.com
PopServer=server2.mail.xyz.com
PopServer=server3.mail.xyz.com

I need to replace all the lines of PopServer=...... with PopServer=No-mail

So from these lines found in the file I need to make them look like
PopServer=No-mail
PopServer=No-mail
PopServer=No-mail

supose I had a file in d:\docs\mail\mail.ini that contained the lines I wanted to replace, how would I do that using a expression to find each line. I can do a replace on PopServer= but that is not doing me any good and not sure how to do this since I do not know what the values will be after the PopServer=....

Thanks for any help with this.
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Dim regx, fso, tsSource, tsDest WholeFile

Set regx = New RegExp
Set fso = CreateObject("Scripting.FileSystemObject")
Set tsSource = fso.OpenTextFile("c:\folder\subfolder\file.txt")
Set tsDest = fso.CreateTextFile("c:\folder\subfolder\newfile.txt")

WholeFile = tsSource.ReadAll
tsSource.Close
Set tsSource = Nothing

With regx
    .Global = True
    .IgnoreCase = True
    .Pattern = "PopServer=[\._a-z0-9]+"
    .Replace WholeFile, "PopServer=No-mail"
End With

tsDest.Write WholeFile
tsDest.Close
Set tsDest = Nothing

Set regx = Nothing
Set fso = Nothing

MsgBox "Done"
Avatar of bt707

ASKER

matthewspatrick,

I get a error with that, not sure what I need to do for it.

line 8
Input past end of file


Thanks,
Avatar of bt707

ASKER

I was using the code below for replacing the PopServer=... which does work but only replaces the first one it finds, then I found out some of the files has multiple lines in it that needs to be replaced. I was trying to redo it with a regx or figure out how to edit this to make it replace all lines it finds.

Const Reading = 1
Const Writing = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(D:\docs\mail\mail.ini, Reading)
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
 
    intLineFinder = InStr(strNextLine, "PopServer")
    If intLineFinder <> 0 And Not blnFound Then
        strNextLine = "PopServer=No-Check-Mail-For-Eudora"
        blnFound = True
    End If
    strNewFile = strNewFile & strNextLine & vbCrLf
Loop
objTextFile.Close

Avatar of bt707

ASKER

I did get it working with the following, I just had one question on this now.

In this file I'm looking for any line that contains PopServer, which it works fine, but does not work if the PopServer happens to be a different case such as popserver.

Is there a way to make this ignore the case?

Thanks,


Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("D:\DOCUMENTS\Mail\eudora.ini", ForReading)

Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline

    intLineFinder = InStr(strNextLine, "PopServer")
    If intLineFinder <> 0 Then
        strNextLine = "PopServer=NO_Mail"
    End If

    strNewFile = strNewFile & strNextLine & vbCrLf
Loop

objTextFile.Close

Set objTextFile = objFSO.OpenTextFile("D:\DOCUMENTS\Mail\eudora.ini", ForWriting)

objTextFile.WriteLine strNewFile
objTextFile.Close

intLineFinder = InStr(strNextLine, Lcase("PopServer"))

Cheers

Dave
ASKER CERTIFIED SOLUTION
Avatar of Dave
Dave
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bt707

ASKER

brettdj,

That's what I needed.

Thanks,