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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create/overwrite a file

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
What is the best way to write to a file but also if the file doesn't exist create it.

At the moment I have

Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.CreateTextFile ("\\myserver\users\intranets\myintranet\assets2\wslist.txt", ForWriting)

This works the first time I run the script but after that I get an error:

Microsoft VBScript runtime error '800a003a'

Thanks very much

Ed

File already exists
 
Ok almost there now. It works the first time but the 2nd time I get an error on the line

set ts = fso_OpenTextFile(filepath, ForWriting, true)

Microsoft VBScript runtime error '800a0005'

Invalid procedure call or argument

So far I have:

Set fso = CreateObject("Scripting.FileSystemObject")

filepath="\\myserver\users\intranets\myintranet\assets2\wslist.txt"

If fso.FileExists(filepath) Then

set ts = fso_OpenTextFile(filepath, ForWriting, true)
ts.write report

Else

Set ts = fso.CreateTextFile (filepathForWriting)
ts.write report

End If

ts.close
Response.Write("Done")
 
[1] You must not have defined ForWriting.
[2] If you use third param of opentextfile to true, don't/need not bother test existence. It is redundant.
[tt]
const ForWriting=2
Set fso = CreateObject("Scripting.FileSystemObject")
filepath="\\myserver\users\intranets\myintranet\assets2\wslist.txt"
set ts = fso_OpenTextFile(filepath, ForWriting, true)
ts.write report
ts.close
set ts=nothing
set fso=nothing
Response.Write("Done")
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top