How to create directories when uploading to a web server?

Posted on 2005-04-25
Last Modified: 2010-04-07
I want to be able to upload a file to my webserver to a directory named for the current day (i.e. 4-25-2005).
I need it to first check and see if the directory is there.  If it is there then just save the file to it.
If it isn't there then it needs to create the directory and then save the file to that created directory.  
I am able to write to the directory if I manually created it on my web server.  But I don't know how to create the directory if it isn't there.

Here is my code behind.

Private Sub Submit1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick
        Dim myConn As New SqlClient.SqlConnection
        Dim myCommand As New SqlClient.SqlCommand
        Dim myAdapter As New SqlClient.SqlDataAdapter
        Dim CS As String

        CS = "Server=DELL;initial catalog=classes;integrated security=true"
        myConn.ConnectionString = CS
        myCommand.Connection = myConn
        myAdapter.SelectCommand = myCommand
        If Not File1.PostedFile Is Nothing And File1.PostedFile.ContentLength > 0 Then
            Dim dateNow As String = Date.Today
            Dim properDate As String = Replace(dateNow, "/", "-")
            Dim fn As String = System.IO.Path.GetFileName(File1.PostedFile.FileName)
            Dim fileSize As String = File1.PostedFile.ContentLength
            Dim SaveLocation As String = Server.MapPath("Data\" + properDate) & "\" & fn
            myCommand.CommandText = "INSERT INTO FILES(file_name, file_size, file_path, upload_date) VALUES ('" & fn & "', '" & fileSize & "', '" & SaveLocation & "', '" & DateTime.Now & "')"
                Response.Write("The file has been uploaded.")
                lblMessage.Text = "Record Successfully Added"
            Catch Exc As Exception
                Response.Write("Error: " & Exc.Message)
            End Try
            Response.Write("Please select a file to upload.")
        End If
    End Sub
Question by:jettman26
    LVL 9

    Expert Comment

    Hi jettman26,

     Could you not use the directory namespace and check its existance and the create it if its not there!

    LVL 9

    Accepted Solution


    This is code that checks if it exists amd other stuff but I am sure you can hack it to do what you need.

    ' For File.Exists, Directory.Exists

    Imports System
    Imports System.IO
    Imports System.Collections

    Public Class RecursiveFileProcessor

        Public Overloads Shared Sub Main(ByVal args() As String)
            Dim path As String
            For Each path In args
                If File.Exists(path) Then
                    ' This path is a file.
                    If Directory.Exists(path) Then
                        ' This path is a directory.
                        Console.WriteLine("{0} is not a valid file or directory.", path)
                    End If
                End If
            Next path
        End Sub 'Main

        ' Process all files in the directory passed in, recurse on any directories
        ' that are found, and process the files they contain.
        Public Shared Sub ProcessDirectory(ByVal targetDirectory As String)
            Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
            ' Process the list of files found in the directory.
            Dim fileName As String
            For Each fileName In fileEntries

            Next fileName
            Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
            ' Recurse into subdirectories of this directory.
            Dim subdirectory As String
            For Each subdirectory In subdirectoryEntries
            Next subdirectory

        End Sub 'ProcessDirectory

        ' Insert logic for processing found files here.
        Public Shared Sub ProcessFile(ByVal path As String)
            Console.WriteLine("Processed file '{0}'.", path)
        End Sub 'ProcessFile
    End Class 'RecursiveFileProcessor
    LVL 9

    Expert Comment


    for creating

    Dim di As DirectoryInfo = Directory.CreateDirectory(path)

    Author Comment

    Worked great.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    732 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

    20 Experts available now in Live!

    Get 1:1 Help Now