Hi Dw

I'm trying to copy a chosen file to a directory under 'C:\example' directory folder, I have a textbox to store the address (path) to the selected file, a OpenFileDialog to enable the user to browse to the file to copy and also a FolderBrowserDialog which let the user browse the folder he would like to copy. There are 2 radiobuttons which are for determining which tool to use for browsing between the Folder and a File Dialog so the first radiobutton if it checked the system will use the OpenFileDialog and if the RadioButton2 is checked then the system will us the FolderBrowserDialog. I have two buttons the first button is for browsing which works fine and the second button is for copying the chosen file to 'C:\example' so now when I try to copy it just produce this error

"Could not complete operation since a directory already exist in the path 'C:\example'."

The folder example is created by this program on a first time execution so I don't want to delete it because it may contain the user files which the user has copied so I want to add item to the folder. Here is the code I'm using:

If My.Computer.FileSystem.DirectoryExist("C:\example" Then
My.Computer.FileSystem.FileCopy(TextBox1.Text.ToString,"C:\example")

' This produced the above error and I tried to swap around like below and I got the following error.

' Note that below I commented the above code of copying a file to use the below.

Dim Dest As String = "C:\example"
FileCopy(TextBox1.Text.ToString, Dest)
MsgBox("File Copied")
End If

I then get this error " The target file 'C:\example' is a directory, not a file."

Sorry for not formating the codes I'm using a mobile phone.

Thank you.

in the destination, you need to put the file directory not the folder that contains it.

My.Computer.FileSystem.FileCopy(your file,new file directory)
' ex: i want to copy this file(C:\sound.wav) to (c:\music)
My.Computer.FileSystem.FileCopy(C:\sound.wav,c:\music\sound.wav)

You didn't provide the name for the file in the destination

My.Computer.FileSystem.CopyFile("C:\Users\coder\Desktop\EXCEL PROJECT\excel\1_2012.xlsx", My.Computer.FileSystem.SpecialDirectories.Desktop & "\TestCopy.xlsx", True)

Click Here

" The target file 'C:\example' is a directory, not a file."

you should do something like this:

Sub CopyFile(ByRef SourceFile as String, ByRef DestinationPath as string, Optional ByVal OverWrite as boolean = false)
dim FileName as String
dim ExtnPoint, Indx as integer
dim FilePath as string
if instrRev(SourceFile, "\") = 0 then
    MessageBox.show("Source File not complete path!")
    exit sub
else
    FileName = mid(SourceFile, instrRev(SourceFile, "\")+1)
end if

ExtnPoint = instrRev(FileName, ".")
if extnPoint = 0 then
    Messagebox.show ("The File String is invalid, there is no extension")
    exit sub
end if

if directory.Exists(DestinationPath) = False then
    'The Destination does not exist so create it
    directory.Create(DestinationPath)
    'Else - so what? we already have it
end if

if DestinationPath.EndsWith("\") = false then
    DestinationPath += "\" 'for appending the file to
end if

FilePath = DestnationPath & FileName
If File.Exists(FilePath) andAlso OverWrite = false then
    'if we don't wish to over write but wish to copy we can add an index to the file name
    'e.g. MyFile.txt  becomes MyFile(1).txt
    Indx=1
    While File.Exists(FilePath)
        FilePath = DestnationPath &Left(FileName, ExtnPoint-1) &"(" &Indx &")" &mid(FileName,ExtnPoint) 
        Indx+=1
    End While
end if

File.Copy(SourceFile, FilePath, OverWrite)

Messagebox.Show (Sourcefile & " Copied to " & FilePath)
End Sub

Ok Now I get what you'll saying that I should specify the name of the file when it is copied now the problem is how can I keep the same name or original name as from source where the user chosen the file from? E.g if the file is located in Desktop for instance and the file name is test1.txt and the user browse to this file and select it to copy it to C:\example how to keep the same name test1.txt to the destination? Keep in mind that the path to the file will only be determined when the user Browse to the file so I can't write manual the path to the file because I don't know where the user will browse to for files to copy to the folder example.

Quick and dirty demo

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim filePathAndName As String = SelectFile()

        If filePathAndName IsNot Nothing Then
            Dim dir As String = GetFolder()
            If dir IsNot Nothing Then
                My.Computer.FileSystem.CopyFile(filePathAndName, dir & "\" & _
                                                System.IO.Path.GetFileName(filePathAndName), True)
            End If
        End If
    End Sub

    Private Function GetFolder() As String

        Dim fldr As String = Nothing

        With fbd1
            .Description = "SELECT DIRECTORY FOR EXCEL FILE"
            .ShowNewFolderButton = True
            Dim dlgResult As DialogResult = .ShowDialog()
            If dlgResult = Windows.Forms.DialogResult.OK Then
                fldr = .SelectedPath
            Else
                MsgBox("EXCEL FILE WASN'T SAVED")
                fldr = Nothing
                Return fldr
                Exit Function
            End If

        End With


        Return fldr

    End Function


    Private Function SelectFile() As String
        Dim file As String = Nothing

        With ofd1
            .InitialDirectory = "c:\"
            .Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
            .Multiselect = False
            .RestoreDirectory = True
            If .ShowDialog = DialogResult.OK Then
                file = .FileName
            End If
        End With

        Return file

    End Function



End Class

E.g if the file is located in Desktop for instance and the file name is test1.txt and the user browse to this file and select it to copy it to C:\example how to keep the same name test1.txt to the destination?

My example allows for this, here are the scenarios that my example handles:

  1. There is no file called "test1.txt" in the c:\example directory > copy over the file.
  2. There is a file called "test1.txt" in the c:\example directory and overwrite is true, overwrite the existing file with the "copying" file.
  3. There is a file called "test1.txt" in the c:\example directory and overwrite is false, copy the file as "test1(1).txt" to directory - or you could just flag up a messagebox and allow the user to specify a new name or quit.

Thanks now the problem has been solved.

If your problem is solved you should mark it as such.

Jea I'm currently having problems with selecting the Mark Question Solved button on a mobile it just refreshes the page so I will mark all my solved questions using a desktop. I think I should forward all the problems I'm experiencing with Daniweb on a mobile to administrator but I don't know how to reach them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.