-1

I am having a asp web page..in that i am having upload options...if the user enters the invoice number and they redirect to upload page..from there they upload all the documents of that particular invoice and it saved in a path which i gave it in the code..

What i need now is i need to generate folder in the name of that invoice number of the user entered and all the uploaded document will be saved in that folder..

I need to check if a directory exists, and if not then create it.but I am struggling with how to do this in classic ASP.

any help is really appreciated

this is my asp code

Public Sub SaveToDisk(sPath)
        Dim oFS, oFile
        Dim nIndex

        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

        oFile.Close
    End Sub
Flakes
  • 2,381
  • 8
  • 25
  • 28
Affan
  • 349
  • 2
  • 3
  • 15
  • 1
    have you tried the `CreateFolder` method of `FileSystemObject` – Flakes Apr 11 '13 at 08:00
  • please do a search on createFolder , you will get a lot of results. – Flakes Apr 12 '13 at 09:59
  • using that createFolder i am able to create it..but I need to upload a file to a folder wich might not exist yet. How can i create a folder before uploading the file or is there a parameter in asp that creates a folder before copying the file there if it doesn't exit? – Affan Apr 15 '13 at 05:40

1 Answers1

2

To check if a folder exists, and create it if it doesn't you could use something like this:

Dim oFS, oF
Set oFS = Server.CreateObject("Scripting.FileSystemObject")
If Not oFS.FolderExists(sPath) Then
    Set oF = fs.CreateFolder(sPath)
    Set oF = Nothing
End If
Set oFS = Nothing
johna
  • 9,832
  • 13
  • 42
  • 65
  • thank you john..but i have to Create a folder name based on user input which is get from the form.. – Affan Apr 13 '13 at 10:41
  • You can change sPath for any folder name but you must include the full path (or use Server.MapPath). – johna Apr 13 '13 at 23:44
  • I need to upload a file to a folder wich might not exist yet. How can i create a folder before uploading the file or is there a parameter in asp that creates a folder before copying the file there if it doesn't exit? – Affan Apr 15 '13 at 04:33
  • It won't automatically create a folder for you by copying or uploading. You have to check if the folder exists (use FolderExists)and create the folder first using CreateFolder. – johna Apr 15 '13 at 09:03