Solved

Can't create a file in wwwroot folder dynamically

Posted on 2002-06-23
8
343 Views
Last Modified: 2007-12-19
Hello All,

I'm not able to create a file in my virtual folder using File System object. But i'm able to create the file outside wwwroot dir. The error i'm getting is object required. Please guide me in this regard.

Please check the following code:
-----------------------------------
FileData = "lskdjf laskjdfl askdfj lasdkfj l"
FileName = "test1.doc"

call SaveToDisk("c:\inetpub\") 'this works
call SaveToDisk("c:\inetpub\wwwroot\xyz") 'this doesn't work


Sub SaveToDisk(sPath)
Dim oFS, oFile
Dim nIndex
on error resume next
If sPath = "" Or FileName = "" Then Exit Sub
If Mid(sPath, Len(sPath)) > "\" Then sPath = sPath & "\"

Set oFS = Server.CreateObject("Scripting.FileSystemObject")
If Not oFS.FolderExists(sPath) Then Exit Sub
     
Set oFile = oFS.CreateTextFile(sPath & FileName, True)
         
     For nIndex = 1 to LenB(FileData)
         oFile.Write Chr(AscB(MidB(FileData,nIndex,1)))
     Next

Response.Write "
Error : " & err.Description
Response.Write "
Err No. : " & err.number
Response.Write "
Err source : " & err.Source

     oFile.Close
End Sub
-----------------------------------

Thankx in advance
Ayub
0
Comment
Question by:akayubm
  • 4
  • 3
8 Comments
 
LVL 30

Expert Comment

by:third
Comment Utility
try,


Sub SaveToDisk(sPath)
Dim oFS, oFile
Dim nIndex
on error resume next
If sPath = "" Or FileName = "" Then Exit Sub
If Mid(sPath, Len(sPath)) > "\" Then sPath = sPath & "\"

Set oFS = Server.CreateObject("Scripting.FileSystemObject")
If Not oFS.FolderExists(sPath) Then Exit Sub
   
Set oFile = oFS.CreateTextFile(Server.Mappath(sPath & FileName), True)
         
    For nIndex = 1 to LenB(FileData)
        oFile.Write Chr(AscB(MidB(FileData,nIndex,1)))
    Next

Response.Write "
Error : " & err.Description
Response.Write "
Err No. : " & err.number
Response.Write "
Err source : " & err.Source

    oFile.Close
End Sub
0
 

Author Comment

by:akayubm
Comment Utility
When i use Server.MapPath even then i'm getting the same error, i.e Object required [VB Script runtime error] error no. 424.
0
 
LVL 22

Expert Comment

by:CJ_S
Comment Utility
Which ASP version is available on your server?
0
 
LVL 30

Expert Comment

by:third
Comment Utility
when you use server.mappath you need to change your procedure call to,

SaveToDisk "xyz"

where "xyz" is a virtual path in your server.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:akayubm
Comment Utility
I'm using IIS 5.0 on Windows 2000 Advance Server.
With VBScript 5.1

And Mr. third.. can u give me an example as to how to enter a virtual path. Like i have a virtual directory called "cr" mapped to physical dir "cr" in the web server. I want to create the files in this folder. Then how do i give the path. Plz help.

Thankx in advance
Ayub
0
 
LVL 30

Accepted Solution

by:
third earned 250 total points
Comment Utility
ok use this,

<%

FileData = "lskdjf laskjdfl askdfj lasdkfj l"
FileName = "test1.doc"

Sub setTxtFile(textfile, newtxt)
  Const fsoForWriting = 2

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

  'Open the text file
  Dim objTextStream
  Set objTextStream = objFSO.OpenTextFile(textfile, fsoForWriting, True)

  'Write to the text file. Overwrite existing text
  objTextStream.Write newtxt
 
'  response.write "Save Succesful!"
 
  'Close the file and clean up
  objTextStream.Close
  Set objTextStream = Nothing
  Set objFSO = Nothing
End Sub

response.write Server.Mappath("cr\"&Filename)
setTxtFile Server.Mappath("cr\"&Filename), FileData
%>
0
 

Author Comment

by:akayubm
Comment Utility
thanx Mr. third
0
 

Author Comment

by:akayubm
Comment Utility
thanx Mr. third
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now