Solved

vbs - replace line in file

Posted on 2008-10-11
7
962 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 

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

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!

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

628 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