> > New to VB.
> >
[quoted text clipped - 40 lines]
>
> Rick
> 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