?
Solved

How to create directories when uploading to a web server?

Posted on 2005-04-25
4
Medium Priority
?
200 Views
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.
HOW DO I DO THIS?

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"
        'uid=USER_NAME;pwd=PASSWORD;"
        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 & "')"
            Try
                File1.PostedFile.SaveAs(SaveLocation)
                Response.Write("The file has been uploaded.")
                myConn.Open()
                myCommand.ExecuteNonQuery()
                lblMessage.Text = "Record Successfully Added"
                myConn.Close()
            Catch Exc As Exception
                Response.Write("Error: " & Exc.Message)
            End Try
        Else
            Response.Write("Please select a file to upload.")
        End If
    End Sub
0
Comment
Question by:jettman26
  • 3
4 Comments
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 13863798
Hi jettman26,

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

Cheers!
0
 
LVL 9

Accepted Solution

by:
cyberdevil67 earned 1400 total points
ID: 13863803
jettman26,

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.
                ProcessFile(path)
            Else
                If Directory.Exists(path) Then
                    ' This path is a directory.
                    ProcessDirectory(path)
                Else
                    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
            ProcessFile(fileName)

        Next fileName
        Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
        ' Recurse into subdirectories of this directory.
        Dim subdirectory As String
        For Each subdirectory In subdirectoryEntries
            ProcessDirectory(subdirectory)
        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
0
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 13863806
jettman26,

for creating

Dim di As DirectoryInfo = Directory.CreateDirectory(path)
0
 

Author Comment

by:jettman26
ID: 13863933
Thanks!
Worked great.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses

621 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