How to copy a file from one directory to another directory, creating a folder if this folder does not exist

I have a problem copying a file from one directory to another directory, creating a folder if this folder does not exist in the destination directory.

Example:

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

If C:\Data\ does not contain the "temp" or "test" folder, it must create a folder before proceeding with 1.txt .

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

Below is my code. But that does not 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 
+6
source share
2 answers

Below is not a catalog.

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

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

In addition, I recommend telling you more about your questions next time. Significant exceptions occur in the code, such as a DirectoryNotFoundException with the corresponding path in the form of a message or (if the file exists) an IOException with the message "Invalid directory name". You should have added this to the question.

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

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

If you need to copy only one file, use the 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) 
+9
source
  Dim strMasterResourceDirectory As String Dim strDirectory As String strDirectory = "C:\TestDestination" strMasterResourceDirectory = "TestResource" If My.Computer.FileSystem.DirectoryExists(strDirectory) = False Then My.Computer.FileSystem.CreateDirectory(strDirectory) End If ' Loop through each file in the directory For Each file As IO.FileInfo In New IO.DirectoryInfo(strDirectory).GetFiles If file.Name <> "Thumbs.db" Then System.IO.File.Delete(strDirectory & "\" & file.Name) End If Next ' Loop through each file in the directory For Each file As IO.FileInfo In New IO.DirectoryInfo(strMasterResourceDirectory).GetFiles If file.Name <> "Thumbs.db" Then ' copy resource to users local directory file.CopyTo(strDirectory & "\" & file.Name) End If Next 
0
source

Source: https://habr.com/ru/post/908015/


All Articles