Link to home
Start Free TrialLog in
Avatar of bylj
bylj

asked on

File Recreation

Have a file in (Server.Mappath("folder1\file1.asp")
that re-creates, writes and closes a file:

Code:
*********************************************************

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

If fs.FileExists(Server.Mappath("folder2\file2.inc")) Then
   fs.deletefile(Server.Mappath("folder2\file2.inc"))
End If

Set file = fs.CreateTextFile(Server.Mappath("folder2\file2.inc"))
file.WriteLine "hello"
file.Close

*********************************************************

Points:
1. Any user can access file2.asp(ie. not only admins, any user)
2. file.inc is NOT readonly

Error:
Permission Denied!!!

I used to get this error every now and then but lately its been frequent. And eventually the error would disappear if I run the file couple of times(File2.asp is a source for a Submit-bttn), so by clicking the button continously for 5-10times, the error disappears. But I'm back to error when I restart browser.

The newly created file's extension has to be "inc" as its used in some other .asp-files.

Tried:
Set file = fs.CreateTextFile(Server.Mappath("folder2\file2.inc"), true)

Still no luck...

Help needed!!!
Avatar of weesiong
weesiong

bylj,

You are no permisssion to delete the file, i think so....

But try to overwrite the file using this:

Set fso = CreateObject("Scripting.FileSystemObject")

  f.Write "Hello world!"
  f.Close

If fs.FileExists(Server.Mappath("folder2\file2.inc")) Then
Set f = fso.OpenTextFile((Server.Mappath("folder2\file2.inc")), ForWriting, True)
file.WriteLine "hello"
file.Close
End If


Regrads,
Wee Siong

ASKER CERTIFIED SOLUTION
Avatar of weesiong
weesiong

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
add in these 2 lines after your codes. Hope it helps.

set fs = nothing
set file = nothing

good luck.
Avatar of Michel Sakr
Check if the IUSR_<webservername> have change permissions on all the files needed to be deleted.. also this user needs write (change) permission in the directory where you are creating the file.. if the IUSR have only read permission you'll get such errors.. IUSR is a local account on the webserver.. you can check permissions by right clicking the affected folder/file -> propretie-> security...
There is no need to delete that file ok

reopen the file in writting mode. It will automatically over write that file.

I think now ur error will be erase.
Avatar of bylj

ASKER

ianouii: Where does your suggested 2-lines go? (Before file.close or after?)

shirjeel: No need to delete the file(I agree). Reopen the file in writing-mode.(HOW DO I DO THAT? WHAT IS THE CODE?)
Set f = fso.OpenTextFile((Server.Mappath("folder2\file2.inc")), ForWriting, True)
Avatar of bylj

ASKER

weesiong: Tried your suggestion, but got error:

"Invalid procedure call or argument"
Avatar of bylj

ASKER

weesiong: Sorry, should be:
Set f = fso.OpenTextFile((Server.Mappath("folder2\file2.inc")), 2, True)

My mistake: Forgot to declare a value for constant-ForWriting.

Again:
ianouii: Where does your suggested 2-lines go? (Before file.close or after?)

shirjeel: No need to delete the file(I agree). Reopen the file in writing-mode.(HOW DO I DO THAT? WHAT
IS THE CODE?)
Set f = fso.OpenTextFile((Server.Mappath("folder2\file2.inc")), ForWriting)
byli,

What the way i am using is no need to Delete the file, just overwrite the Text in your .inc file!

So it can work, in my first suggestion script?

Regrads,
Wee Siong ^__^
Permissions.. permissions.
bylj---->  You have asked 57 questions at this site and only closed 39 of them, which is against our site's Guidelines.  Please click the HELP DESK link on the left regarding Member Agreement, Guidelines and the Question/Answer process.  I will be updating all your open questions with this information and monitor them for closures.  Please return as quickly as possible to complete these questions.  If special handling is needed, comment here with details.

Open today:
https://www.experts-exchange.com/jsp/qShow.jsp?ta=oracle&qid=20091636
https://www.experts-exchange.com/jsp/qShow.jsp?ta=win2k&qid=20241395
https://www.experts-exchange.com/jsp/qShow.jsp?ta=win2k&qid=20241043
https://www.experts-exchange.com/jsp/qShow.jsp?ta=dbgen&qid=20247085
https://www.experts-exchange.com/jsp/qShow.jsp?ta=msaccess&qid=20237668
https://www.experts-exchange.com/jsp/qShow.jsp?ta=oracle&qid=20088755
https://www.experts-exchange.com/jsp/qShow.jsp?ta=hardgen&qid=20228196
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20248490
https://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20108859
https://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20091635
https://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20088839
https://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20087841
https://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20085748
https://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20081510
https://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20081065
https://www.experts-exchange.com/jsp/qShow.jsp?ta=msvisdev&qid=20088291
https://www.experts-exchange.com/jsp/qShow.jsp?ta=networkgen&qid=20220006
https://www.experts-exchange.com/jsp/qShow.jsp?ta=networkgen&qid=20198006

It helps you tremendously to remain active in all your open questions, provide ongoing feedback to the experts who step in to help you so that your goal can be achieved quickly, and the expert(s) awarded with points for their excellent help.

Your responsiveness to these is appreciated.

Moondancer
Community Support Moderator @ Experts Exchange
Admin notified of User neglect. Force-accepted by
Netminder
Community Support Moderator
Experts Exchange