Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

ASP want to overwrite or delete contents of file before writing to it

Posted on 2006-05-16
3
Medium Priority
?
269 Views
Last Modified: 2012-06-27
I have a form that sends the data to a text file. The data currently gets written in the text file below the last entry
What I would like to do is only have the current data populate that file. So I must delete the file and create a new one
or overwrite the data in the current file.

Here is my code

<%
                  If Request.Form("action") = "approved" then
path = "C:/textfolder/"
'after insert database :
path =path & stros
stros = request.form("hcodeline")
set fs = server.createobject("scripting.filesystemobject")
set txt = fs.opentextfile(path&stros&".txt",8,true,false)
txt.writeline stros
txt.writeline strcmpname
txt.writeline strBuild
txt.writeline strCis
txt.close
set fs = nothing

End If

Thanks
Tmurray22
0
Comment
Question by:tmurray22
  • 2
3 Comments
 
LVL 19

Expert Comment

by:peh803
ID: 16691779
To delete the file first:

path = "C:/textfolder/"
'after insert database :
path =path & stros
stros = request.form("hcodeline")
set fs = server.createobject("scripting.filesystemobject")

'' CODE INSERTED HERE SHOULD DELETE THE FILE FIRST!!!
Set fileObject = fso.GetFile(path&stros&".txt")
fileObject.Delete

set txt = fs.opentextfile(path&stros&".txt",8,true,false)
txt.writeline stros
txt.writeline strcmpname
txt.writeline strBuild
txt.writeline strCis
txt.close
set fs = nothing
0
 
LVL 19

Expert Comment

by:peh803
ID: 16691887
It might be best to check to see if the file exists first, before trying to delete -- so....

if fs.FileExists(path&stros&".txt") then
   '' CODE INSERTED HERE SHOULD DELETE THE FILE FIRST!!!
   Set fileObject = fso.GetFile(path&stros&".txt")
   fileObject.Delete
end if

HTH!
Phil / peh803
0
 
LVL 11

Accepted Solution

by:
walkerke earned 2000 total points
ID: 16693358
Leave your code as is, but change

set txt = fs.opentextfile(path&stros&".txt",8,true,false)

to

set txt = fs.opentextfile(path&stros&".txt",2,true,false)

(change the number 8 to 2). The number 8 sets the open file to "append" mode while the number 2 sets the file to "write" mode. (Write is the technically correct mode, but "overwrite" would be more accurate.)
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question