Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CreateTextFile

Status
Not open for further replies.

FiberGhost

Instructor
Feb 6, 2002
3
US
I am using the following code to write to the Hosts file on a computer. Earlier code figures out what OS and then path to the Hosts file. If the file does not exist I want it to create the file and add two lines. The code is:

'If CheckFileExist is False then create the file.
If CheckFileExists = False Then
oFSO.CreateTextFile(sHostsPath,true).WriteLine("192.1.1.2 mris_sv1")
oFSO.CreateTextFile(sHostsPath,true).WriteLine("10.20.80.241 mris_rws")
'oFSO.CreateTextFile(sHostsPath,true).Close
End If

But only 192.1.1.2 mris_rws is written. If I change it to

'If CheckFileExist is False then create the file.
If CheckFileExists = False Then
oFSO.CreateTextFile(sHostsPath,ForAppending,true).WriteLine("192.1.1.2 mris_sv1")
oFSO.CreateTextFile(sHostsPath,ForAppending,true).WriteLine("10.20.80.241 mris_rws")
'oFSO.CreateTextFile(sHostsPath,true).Close
End If

It only writes the first line. I currently have the last line commented out ('oFSO.CreateTextFile(sHostsPath,true).Close) because if I don't the file is created, but is blank.

What am I doing wrong?

 
Hello FiberGhost,

Just create the text file once if CheckFileExists=False. Then write the lines via the same instructions for the sHostsPath existing or not beforehand.

The relevant addition should something like:
'------------
Dim oFile
Const ForAppending=8
If CheckFileExists = False Then
Set oFile=oFSO.CreateTextFile(sHostsPath,true)
oFile.close
Set oFile=Nothing
End If
Set oFile=oFSO.OpenTextFile(sHostsPath,ForAppending)
oFile.WriteLine "192.1.1.2 mris_sv1"
oFile.WriteLine "10.20.80.241 mris_rws"
oFile.Close
Set oFile=Nothing
'-------------

regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top