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

Using Scripting.FileSystemObject

Status
Not open for further replies.

rewdee

Programmer
Aug 17, 2001
295
0
0
US
I want to first check to see if the specified file is created, if so delete it and then put the contents of my string (simple xml document built using string manipulations into a text file and save as an xml extension. If tried this code:
Code:
dim fso, TextFile
set fso = createobject("Scripting.FileSystemObject")
set TextFile = objfso.createtextfile("c:\temp.xml")
On Error Resume Next
TextFile.Delete
set TextFile = objfso.createtextfile("c:\temp.xml")
TextFile.writeLine(sXML)
TextFile.Close
Nothing seems to be happening but I'm sure this code can be improved. Any pointers and hints would be greatly appreciate.

Thanks,
Rewdee
 
try this:
Code:
<%
strFile = "C:\Temp.xml"
sXML = "some text here"

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

Set objTextFile = objFSO.CreateTextFile(strFile, ForWriting, True)

objTextFile.WriteLine(sXML)

Set objTextFile = Nothing
Set objFSO = Nothing
%>
You don't need to delete the file first as the contents will just get overwritten due to the [red]ForWriting[/red] parameter. If you use [red]ForAppending[/red] instead then the script would add the text to the text file rather than replace what is there already.

[red]ForWriting[/red] and [red]ForAppending[/red] are both ADO constants and are present in the msado15.dll. Use the following line to include this file:
Code:
<!-- METADATA TYPE="typelib" FILE="C:\Program Files\Common Files\System\ado\msado15.dll" -->

Tony
_______________________________________________________________
 
Thanks Tony for the response. It works great except:
1)Writes to server where I want to write to the users machine not the server.
2)Maybe because it's on the server and the server has restrictions but when I ran the script the second time I got this message:

Microsoft VBScript runtime error '800a003a'
File already exists
/reports/My_ASP_Page.asp, line 311

Any ideas how to fix this?

Thanks,
Rewdee
 
>>1)Writes to server where I want to write to the users machine not the server.

writing to client system cannot be done, u can allow the client to maybe download the file written on the server...

>>2)Maybe because it's on the server and the server has restrictions but when I ran the script the second time I got this message:

thats because the file already exists. to overcome this problem u can use the FileExists() method of the FSO object:

if objFSO.FileExists("C:\Temp.xml") then
objFSO.DeleteFile "C:\Temp.xml"
end if

Set objTextFile = objFSO.CreateTextFile(strFile, ForWriting, True)




Known is handfull, Unknown is worldfull
 
strange I didnt get that error.

you could add [red]this code[/red] to make sure the file is deleted each time if you like:
Code:
...
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

[red]If objFSO.FileExists(strFile) Then
  objFSO.DeleteFile(strFile)
End If[/red]
...
As for writing the file to the clients machine - I don't think you can do that - might be wrong though

Tony
_______________________________________________________________
 
Well you could try it in client side script but the user will probably get some sort of nasty warning message.
 
my previous post should have had this line instead:
Code:
[red]objFSO.DeleteFile strFile, True[/red]

Tony
_______________________________________________________________
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top