Solved

VB6 text file manipulation

Posted on 2007-04-06
13
646 Views
Last Modified: 2013-12-25
I am new to VB6 and need to create a button that will make a change to an entry in a file. There will be three of these buttons. After the changes are made then a fourth will execute a script file that does other things. Does anyone have any sample code that I can use for these buttons?
0
Comment
Question by:TCHURCH65
[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
  • 6
  • 6
13 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18865360
What sort of change?
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 18865373
VB6:  Add a reference to the Microsoft Scripting Runtime Library (scrrun.dll) Project / References.  

Then, code like this:
Dim fso as new FileSystemObject
Dim ts as TextStream
Dim inLine as string

'To Read:
set ts = fso.OpenTextFile("c:\temp\myfile.txt", ForReading)
Do
     inLine = ts.ReadLine
     'Process with your line.
Loop Until ts.AtEndOfStream = True
ts.close

'To Write:
set ts = fso.OpenTextFile("c:\temp\myfile.txt", ForWriting)
ts.WriteLine "Cool!"
ts.Close
0
 

Author Comment

by:TCHURCH65
ID: 18865542
I'm getting an error that state "User defined type not defined" and points out this

Dim fso As New FileSystemObject

The file is a router config file and I need this to search and allow change to hostname, ipaddress and dlci i have the interface and the 3 buttons created but i have no idea about what code to put in. Ideally it would be nice to click the button and then have a pop up asking for the entry and so on.....
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!

 
LVL 67

Expert Comment

by:sirbounty
ID: 18865576
How about
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
FileData=objFSO.OpenTextFile("C:\SourceFile.txt").ReadAll
OldHost="something"
OldIP="111.22.33.44"
OldDlci="something"

strHost=InputBox("Enter the new HostName")
strIP=InputBox("Enter the new IP")
strDlci=InputBox("Enter the new dlci")

Set NewFile=objFSO.CreateTextFile("C:\TargetFile.txt")
NewFile.Write Replace(Replace(Replace(FileData,OldHost, strHost), OldIP, strIP), OldDlci, strDlci)
NewFile.Close

Set NewFile=Nothing
Set objFSO=Nothing
0
 

Author Comment

by:TCHURCH65
ID: 18865756
Almost there.....it won't make the change to the new file.  Like the hostname, ip addrress, dlci
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18865789
How's the file laid out?
I was under the impression that those values were already there?
If you filled in the 'old' variables, it should simply replace them...
Can you post a copy? (mask the data if you need to)
0
 

Author Comment

by:TCHURCH65
ID: 18865837
The file is just a common cisco config file :

for example:
hostname c2600

I really appreciate your help
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18865941
Haha - common to many people, maybe, but I haven't worked with Cisco config files in years... :^)

Are you updating these lines?
OldHost="c2600" '<<<
OldIP="111.22.33.44" '<<<
OldDlci="something" '<<<
0
 

Author Comment

by:TCHURCH65
ID: 18866228
Yesssss...............

Very sorry....
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 125 total points
ID: 18866294
Does the host get altered with this version?

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
FileData=objFSO.OpenTextFile("C:\SourceFile.txt").ReadAll
OldHost="something"
OldIP="111.22.33.44"
OldDlci="something"

strHost=InputBox("Enter the new HostName")
strIP=InputBox("Enter the new IP")
strDlci=InputBox("Enter the new dlci")

Set NewFile=objFSO.CreateTextFile("C:\TargetFile.txt")
NewFile.Write Replace(FileData,OldHost, strHost)
NewFile.Close

Set NewFile=Nothing
Set objFSO=Nothing
0
 

Author Comment

by:TCHURCH65
ID: 18866371
Sorry. I creates the file but no changes made.
0
 

Author Comment

by:TCHURCH65
ID: 18866412
I just tried it again. It worked. My fault. I had to change the name of the varialbe to c2600
You got it!
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18866430
:^)
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 11 hours left to enroll

617 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