Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy a file from one directory to another directory by creating the folder if that folder does not exist

I have some problem with copying the file from one directory to another directory by creating the folder if that folder does not exist in the destination directory.

Example:

  • Source path: C:\temp\test\1.txt
  • Destination path: C:\Data\

If C:\Data\ doesn't contain "temp" or "test" folder, it should create the folder before coping 1.txt.

Copied to C:\Data\temp\test\1.txt

Below is my code. But it doesn't work..

Private Sub btnBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackup.Click
          Dim sourcepath As String = "C:\temp\test\1.txt"
    Dim DestPath As String = "C:\Data\"
    CopyDirectory(sourcepath, DestPath)
End Sub

Private Shared Sub CopyDirectory(sourcePath As String, destPath As String)
    If Not Directory.Exists(destPath) Then
        Directory.CreateDirectory(destPath)
    End If

    For Each file__1 As String In Directory.GetFiles(sourcePath)
        Dim dest As String = Path.Combine(destPath, Path.GetFileName(file__1))
        File.Copy(file__1, dest)
    Next

    For Each folder As String In Directory.GetDirectories(sourcePath)
        Dim dest As String = Path.Combine(destPath, Path.GetFileName(folder))
        CopyDirectory(folder, dest)
    Next
End Sub
like image 369
user1101157 Avatar asked Feb 09 '12 08:02

user1101157


People also ask

How do I automatically copy files from one folder to another?

You can automatically move files from one folder to another by using a script that uses Robocopy, a command-line utility which comes with Windows 10. To automated file transfer, you need to use Robocopy script, add frequency in days, source and destination folder paths.

How do I copy a file from multiple directories to one directory?

If you want to copy the set of files to multiple folders, hold down the CONTROL key while dragging and dropping the file(s). This ensures that the files in the drop stack are not removed once the copy process has completed. This makes it easy to copy the same set of files (from different folders) to multiple locations.

How do you create a file then copy it to a different directory in Linux?

The Linux cp command is used for copying files and directories to another location. To copy a file, specify “cp” followed by the name of a file to copy. Then, state the location at which the new file should appear. The new file does not need to have the same name as the one you are copying.


1 Answers

The following is not a directory.

Dim sourcepath As String = "C:\temp\test\1.txt"

Because you are using it as a directory in Directory.GetFiles(sourcePath).

Apart from that, I recommend to elaborate your questions more the next time. The code raises meaningful exceptions like DirectoryNotFoundException with the appropriate path as message or (if the file exists) an IOException with message "The directory name is invalid". You should have added that to the question.

So the solution simply is to remove the 1.txt from the directory-name:

Dim sourcepath As String = "C:\temp\test\"

If you need to copy only one file, use CopyTo method:

Dim sourcepath As String = "C:\temp\test\"
Dim DestPath As String = "C:\temp\Data\"
If Not Directory.Exists(DestPath) Then
    Directory.CreateDirectory(DestPath)
End If
Dim file = New FileInfo("C:\temp\test\1.txt")
file.CopyTo(Path.Combine(DestPath, file.Name), True)
like image 142
Tim Schmelter Avatar answered Oct 04 '22 22:10

Tim Schmelter