Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

Can't create a file in wwwroot folder dynamically

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
akayubm
Asked:
akayubm
  • 4
  • 3
1 Solution
 
thirdCommented:
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
 
akayubmAuthor Commented:
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
 
CJ_SCommented:
Which ASP version is available on your server?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
thirdCommented:
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
 
akayubmAuthor Commented:
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
 
thirdCommented:
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
 
akayubmAuthor Commented:
thanx Mr. third
0
 
akayubmAuthor Commented:
thanx Mr. third
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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