Solved

vbs - replace line in file

Posted on 2008-10-11
7
947 Views
Last Modified: 2010-04-21
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.
0
Comment
Question by:bt707
  • 4
  • 2
7 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 22694182
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"
0
 

Author Comment

by:bt707
ID: 22695201
matthewspatrick,

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

line 8
Input past end of file


Thanks,
0
 

Author Comment

by:bt707
ID: 22695219
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

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:bt707
ID: 22695306
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

0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 22695925
intLineFinder = InStr(strNextLine, Lcase("PopServer"))

Cheers

Dave
0
 
LVL 50

Accepted Solution

by:
Dave Brett earned 500 total points
ID: 22696883
... hmmmm

The hazards of posting at 2am without enough coffee. try this

Cheers

Dave
intLineFinder = InStr(LCase(strNextLine), "popserver")

Open in new window

0
 

Author Closing Comment

by:bt707
ID: 31505310
brettdj,

That's what I needed.

Thanks,
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question