Copying Files to an Existing Directory

Dear VB Experts,

I humbly ask for your assistance in solving a problem I'm having with copying a file to an existing directory. I'm using the below code to try and copy a file from an OpenFileDialog to a directory that already exists. The source file path is derived from the OpenFileDialog.FileName property and the destination file path is a directory in the users MyDocuments directory.

When I run the code,

If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

Dim filepath As String = OpenFileDialog1.FileName

Dim destinationpath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Pics\Training\" & TrainingEventNoTextBox.Text

My.Computer.FileSystem.CopyFile(filepath, destinationpath, True)

I get an IO Exception with the error message saying it "could not complete operation since a directory already exists in this path."

Well, I need the directory to exist prior to copying the file into it (and at one time or another, multiple files are going to be copied into the same directory), so I'm really not sure what I'm doing wrong. If anyone can take a look at the code and help me figure out where I'm going wrong, I would REALLY appreciate it.

Thanks in advance for any help,

Tony




Answer this question

Copying Files to an Existing Directory

  • H?LΛ??

    Spotty,

    Thanks very much for the reply! Your analysis was right on. I tried the code and I was able to copy the files without any problems.

    I have just one more question that relates to the Function part of the code. When I copy files now, the code seems to increment the destination directories in a way I can't figure out. I get multiple directories with the Training Event No as the first number in the destination directory and another code generated number for the second. Can you explain a little as to what the Function part of your code does

    I would ultimately like to allow the user to rename the file in the OpenFileDialog (if they choose to). So somehow I need to get the RawFile info and new DestinationFile info all from the OpenFileDialog.

    Thanks again for the help,

    Tony



  • Stephen Provine

    From your code - if the TrainingEventNoTextBox textbox was blank then your destination would have been just the foldername and this would cause the problem. the following code has a check condition for this.

    I'm unsure whether TrainingEventNoTextBox is either a destination file or a folder.

    But the destination has to be a file path and not a folder path - if you want the destination file to be called the same as the source file you need to add this to the string. If you provide a full destination file path it will create the folders required for you to put the file in this path.

    The following code may provide some help. It extracts the filename minus the path from the openfiledialog and appends this onto the destination to ensure that a full destination file path is given.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim openfiledialog1 As New OpenFileDialog

    If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    Dim filepath As String = openfiledialog1.FileName

    Dim RawFile As String = ExtractFilename(filepath)
    Dim sDestinationFile As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Pics\Training\"

    If TrainingEventNoTextBox.Text.length = 0 Then
    sDestinationFile = sDestinationFile & RawFile
    Else
    sDestinationFile = sDestinationFile & TrainingEventNoTextBox.Text "\" & RawFile
    End If

    My.Computer.FileSystem.CopyFile(filepath, sDestinationFile, True)
    End If
    End Sub


    Function ExtractFilename(ByVal instring As String) As String
    If instring.Contains("/") Then
    Return instring.Substring(instring.LastIndexOf("/") - 1)
    Else
    Return instring
    End If
    End Function


  • tfif

    Spotty,

    I did a little tweaking and figured out how to copy all related pictures into a directory associated with the TrainingEventNumber. I just removed the "-1" from the Function part of the code and now all files associated with the TrainingEventNo will be copied into a common directory (if that makes sense).

    Even though the code was not intended to be production code, it gave me a lot of insight and now things work great!



  • Richard Lyon

    For more production code you could use the System.IO.Path.GetFileName method which is much more robust.

    Example here....

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim s As String = "c:\test1\test.xls"
    MsgBox(GetJustFileName(s))

    End Sub

    Function GetJustFileName(ByVal Path As String) As String
    Return System.IO.Path.GetFileName(Path)
    End Function
    End Class


  • Gianni1962PiacenzaIT

    The function was just something to try and extract the filename from the full pathname. As the folder separators are a "\" this will search for everything to the right of the last "\" character and should give me the filename.

    The code was not intended to be production code but this would use the trainingevent field as a sub folder directory and then add the source filename to the destination filename. to give me and entire destination file path. I wasnt sure what the trainingevent textbox was meant to signify.

    Either way you now know that you need to specify a full destination file path. How you do that is down to your specific code.

    If your going to allow the user the change the name when they save then look at the saveFileDialog to allow the user to specify a destination file.

    If your trying to combine both load and save info on the same dialog then your probably going to have to code this functionality yourself.


  • Copying Files to an Existing Directory