• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 918
  • Last Modified:

ASP delete file if already exists

I have an update page that writes field values to a text file.

If the text file exists, it should delete the existing file and create a new one
with the new values.

I am getting a need object error

here is my code

path = "C:/textfolder/"
path =path & stros

set fs = server.createobject("scripting.filesystemobject")

if fs.FileExists(path&".txt") then
   Set fileObject = fso.GetFile(path&".txt")
   fileObject.Delete
end if

set txt = fs.opentextfile(path&".txt",8,true,false)
txt.writeline strcmpname
txt.writeline stros
txt.writeline strBuild
txt.close
set fs = nothing
0
tmurray22
Asked:
tmurray22
  • 4
1 Solution
 
trainsdseCommented:
Change

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

TO

set txt = fso.opentextfile(path&".txt",8,true,false)
0
 
trainsdseCommented:
SCRATCH THAT! ignore my comment
0
 
trainsdseCommented:
Ok, your code works when I hard code the variables, see if this works:

path = "C:/textfolder/"
path =path & "bubba"

set fs = server.createobject("scripting.filesystemobject")

if fs.FileExists(path&".txt") then
   Set fileObject = fso.GetFile(path&".txt")
   fileObject.Delete
end if

set txt = fs.opentextfile(path&".txt",8,true,false)
txt.writeline "mycompany"
txt.writeline "bubba"
txt.writeline "mybuild"
txt.close
set fs = nothing

if so you have some un-populated variables, my guess would be the stros variable since it's used to make or get an object.

Thomas
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
tmurray22Author Commented:
got it to work like this


If (fs.FileExists (path&".txt"))= true  then

Set fileObject = (fs.GetFile(path&".txt"))
   fileObject.Delete


you put me on the right path(no pun intended)
0
 
trainsdseCommented:
LOL, nice one, glad you got it worked out!

Cheers,

Thomas
0
 
GranModCommented:
PAQed with points refunded (500)

GranMod
Community Support Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now