?
Solved

VBS Delete Text from File ?

Posted on 2006-03-20
5
Medium Priority
?
477 Views
Last Modified: 2012-05-05
Hi,

I have a VBS script that creates a VPN connection on the system. Works great. What I want to do now is create another script that will remove that VPN connection that I just created. To do this I want to be able to delete certain text from the rasphone.pbk file. Anyone have any idea how to do this ? Below is the an example of what I am doing to create the connection, all I need to be able to do is the reverse.

Dim filesys, vpnfile
Set filesys = CreateObject("Scripting.FileSystemObject")
Set vpnfile= filesys.OpenTextFile("C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk\rasphone.pbk", 8)
vpnfile.WriteLine "[New VPN Connection]"
vpnfile.WriteLine "Encoding=1"
vpnfile.WriteLine "Type=2"
vpnfile.WriteLine "AutoLogon=0"
vpnfile.WriteLine "UseRasCredentials=1"
vpnfile.WriteLine "DialParamsUID=167104187"
vpnfile.WriteLine "Guid=42B083324A9KGE46B092E887F0F0677C"
vpnfile.WriteLine "BaseProtocol=1"
vpnfile.WriteLine "VpnStrategy=2"
vpnfile.WriteLine "ExcludedProtocols=0"
vpnfile.Close

How can I go back into that text file and Remove those lines I just created ? Any help would be greatly appreciated. Thanks!
0
Comment
Question by:kasowitz
5 Comments
 
LVL 4

Expert Comment

by:dasmcse
ID: 16240793
I would read the rasphone.pbk file line by line copying each line to a temp file and ignoring the lines in question. Then close both files. Then copy the temp over rasphone.pbk.
0
 

Author Comment

by:kasowitz
ID: 16240874
How would I go about doing that in VBS ?  How would I designate which lines to ignore ? Would you mind showing me a little example code please ?  Thanks.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 800 total points
ID: 16241265
Dim filesys, vpnFile, vpnContents, vpnSplit

Set filesys = CreateObject("Scripting.FileSystemObject")

Set vpnFile= filesys.OpenTextFile("C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk\rasphone.pbk", 1)
vpnContents = vpnFile.ReadAll
vpnFile.Close

vpnSplit = Split(vpnContents, "[New VPN Connection]")

Set vpnFile= filesys.OpenTextFile("C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk\rasphone.pbk", 2)
vpnFile.Write(vpnFile(0))
vpnFile.Close
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16241275
This assumes that your "[New VPN Connection]" section is at the end of the file and that there is nothing after it...
0
 
LVL 10

Accepted Solution

by:
fostejo earned 1200 total points
ID: 16241296
kasowitz,

I'd suggest the simplest way is to call rasphone with the -r parameter and the name of the connection to delete; something similar to..

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "rasphone -r TheNameOfYourConnection"

cheers,
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

864 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