Solved

VB6 text file manipulation

Posted on 2007-04-06
13
617 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

895 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

11 Experts available now in Live!

Get 1:1 Help Now