Solved

VB6 text file manipulation

Posted on 2007-04-06
13
605 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

707 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

20 Experts available now in Live!

Get 1:1 Help Now