Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General 2 / July 2006



Tip: Looking for answers? Try searching our database.

Text file help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
termv - 28 Jul 2006 22:02 GMT
New to VB.

I need to read in a text file (C:\text.txt)

Find Line "host.name="
and replace with "host.name=172.29.10.40"

Then

Find Line "host.port="
and replace with "host.port=18505"

My program is all wrapped up with the exception of being able to plug
in this info.

Any help GREATLY appreciated!

Jim
Jennifer - 28 Jul 2006 22:37 GMT
Const Unicode = -1, ASCII = 0

   Dim fso As New FileSystemObject
   Dim fil As TextStream
   Dim TempFile As TextStream
   Dim sFileContents As String
   Dim sFilePath As String
   Dim sOrigName As String
   Dim sTempName As String

   sFilePath = "C:\"
   sOrigName = sFilePath & "Test.txt"
   sTempName = sFilePath & "Temp.txt"

   Set fil = fso.OpenTextFile(sOrigName, ForReading, False, ASCII)
   Set TempFile = fso.OpenTextFile(sTempName, ForWriting, True, ASCII)

   sFileContents = fil.ReadAll
   sFileContents = Replace(sFileContents, "host.name=",
"host.name=172.29.10.40")
   sFileContents = Replace(sFileContents, "host.port=",
"host.port=18505")

   TempFile.Write sFileContents

   fil.Close
   TempFile.Close
   fso.DeleteFile sOrigName, True
   fso.CopyFile sTempName, sOrigName, True
   fso.DeleteFile sTempName

> New to VB.
>
[quoted text clipped - 14 lines]
>
> Jim
Rick Rothstein - 28 Jul 2006 22:45 GMT
> New to VB.
>
[quoted text clipped - 12 lines]
>
> Any help GREATLY appreciated!

Assuming your text file is not several 10's of megabytes large, I would
probably read the entire file into memory all at once, use the Replace
function to do your two substitutions and then write it back out to the hard
drive. Something like this...

 Dim FileNum As Integer
 Dim FileName As String
 Dim TotalFile As String
 FileName = "c:\text.txt"
 FileNum = FreeFile
 '  Reads the entire file into memory all at once
 Open FileName For Binary As #FileNum
   TotalFile = Space(LOF(FileNum))
   Get #FileNum, , TotalFile
 Close #FileNum
 '  Do the two replacements
 TotalFile = Replace(TotalFile, "host.name=", "host.name=172.29.10.40")
 TotalFile = Replace(TotalFile, "host.port=", "host.port=18505")
 '  Write the changed text back to the hard disk
 FileNum = FreeFile
 Open FileName For Output As #FileNum
   Print #FileNum, TotalFile
 Close #FileNum
 '  Release the memory
 TotalFile = ""

Rick
termv - 28 Jul 2006 22:59 GMT
Thanks Rick,
I need to ask one more thing. It may be true that the line BEGINs with
"host.name=".
There may be something more after it already. For instance
"host.name=online1"
And I need to replace the entire line with "host.name=172.29.10.40"
Thanks again for your help!

> > New to VB.
> >
[quoted text clipped - 40 lines]
>
> Rick
Geoff - 31 Jul 2006 01:47 GMT
> Thanks Rick,
> I need to ask one more thing. It may be true that the line BEGINs with
[quoted text clipped - 3 lines]
> And I need to replace the entire line with "host.name=172.29.10.40"
> Thanks again for your help

You can find the entire line with a few changes and using the instr
function,
you might find it better to construct it as a function in case you need to
add/change other settings.

   Call Set_Config_File("c:\text.txt", HostName, "Some.com")
   Call Set_Config_File("c:\text.txt", HostAddress, "172.29.10.40")
   Call Set_Config_File("c:\text.txt", Port, "18505")

Enum Config_Options
   HostAddress
   HostName
   Port
End Enum

Sub Set_Config_File(sFilename$, enumOption As Config_Options, sValue$)
   Dim sTarget As String, sNewFile As String
   Dim startPos As Long, endPos As Long
   Dim TotalFile As String, FileNum As Integer
   FileNum = FreeFile
   '  Reads the entire file into memory all at once
   Open sFilename For Binary As #FileNum
   TotalFile = Space(LOF(FileNum))
   Get #FileNum, , TotalFile
   Close #FileNum
   'Select the option to write
   Select Case enumOption
       Case Config_Options.HostAddress
           sTarget = "HostAddress="
       Case Config_Options.HostName
           sTarget = "HostName="
       Case Config_Options.Port
           sTarget = "Port="
   End Select
   startPos = InStr(TotalFile, sTarget)
   If startPos > 0 Then _
       endPos = InStr(startPos, TotalFile, vbCrLf)
   If startPos = 0 Or endPos = 0 Then
       'Write a new entry
       sNewFile = TotalFile & sTarget$ & sValue & vbCrLf
   Else
       '  Do a replacement
       sNewFile = VBA.Left$(TotalFile, startPos - 1)
       sNewFile = sNewFile & sTarget$ & sValue
       sNewFile = sNewFile & VBA.Right$(TotalFile, Len(TotalFile) -
(endPos - 1))
   End If
   '  Write the changed text back to the hard disk
   FileNum = FreeFile
   Open sFilename For Output As #FileNum
   Print #FileNum, sNewFile;
   Close #FileNum
   '  Release the memory
   TotalFile = "": sNewFile = ""
End Sub

Geoff
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.