Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vbs - replace line in file

Posted on 2008-10-11
7
Medium Priority
?
972 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 93

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 2000 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

Industry Leaders: 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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

715 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