Solved

vbs - replace line in file

Posted on 2008-10-11
7
917 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now