Ghanisen
asked on
Copying all files in a directory to another directory
Hi,
I'd like to know how to copy all files (*.*) in directory DirSource to a directory DirTarget.
I tried hard with Directory.GetFiles(DirSour ce).CopyTo ... but coudn't do it.
This is a beginner's question, sorry.
Any help shall be welcome.
I'd like to know how to copy all files (*.*) in directory DirSource to a directory DirTarget.
I tried hard with Directory.GetFiles(DirSour
This is a beginner's question, sorry.
Any help shall be welcome.
Try this
Sub CopyDirectory(ByVal SourcePath As String, ByVal DestPath As String, Optional ByVal Overwrite As Boolean = False)
Dim SourceDir As DirectoryInfo = New DirectoryInfo(SourcePath)
Dim DestDir As DirectoryInfo = New DirectoryInfo(DestPath)
' the source directory must exist, otherwise throw an exception
If SourceDir.Exists Then
' if destination SubDir's parent SubDir does not exist throw an exception
If Not DestDir.Parent.Exists Then
Throw New DirectoryNotFoundException ("Destinat ion directory does not exist: " + DestDir.Parent.FullName)
End If
If Not DestDir.Exists Then
DestDir.Create()
End If
' copy all the files of the current directory
Dim ChildFile As FileInfo
For Each ChildFile In SourceDir.GetFiles()
If Overwrite Then
ChildFile.CopyTo(System.IO .Path.Comb ine(DestDi r.FullName , ChildFile.Name), True)
Else
' if Overwrite = false, copy the file only if it does not exist
' this is done to avoid an IOException if a file already exists
' this way the other files can be copied anyway...
If Not File.Exists(System.IO.Path .Combine(D estDir.Ful lName, ChildFile.Name)) Then
ChildFile.CopyTo(System.IO .Path.Comb ine(DestDi r.FullName , ChildFile.Name), False)
End If
End If
Next
' copy all the sub-directories by recursively calling this same routine
Dim SubDir As DirectoryInfo
For Each SubDir In SourceDir.GetDirectories()
CopyDirectory(SubDir.FullN ame, System.IO.Path.Combine(Des tDir.FullN ame, SubDir.Name), Overwrite)
Next
Else
Throw New DirectoryNotFoundException ("Source directory does not exist: " + SourceDir.FullName)
End If
End Sub
HTH
Andy
Sub CopyDirectory(ByVal SourcePath As String, ByVal DestPath As String, Optional ByVal Overwrite As Boolean = False)
Dim SourceDir As DirectoryInfo = New DirectoryInfo(SourcePath)
Dim DestDir As DirectoryInfo = New DirectoryInfo(DestPath)
' the source directory must exist, otherwise throw an exception
If SourceDir.Exists Then
' if destination SubDir's parent SubDir does not exist throw an exception
If Not DestDir.Parent.Exists Then
Throw New DirectoryNotFoundException
End If
If Not DestDir.Exists Then
DestDir.Create()
End If
' copy all the files of the current directory
Dim ChildFile As FileInfo
For Each ChildFile In SourceDir.GetFiles()
If Overwrite Then
ChildFile.CopyTo(System.IO
Else
' if Overwrite = false, copy the file only if it does not exist
' this is done to avoid an IOException if a file already exists
' this way the other files can be copied anyway...
If Not File.Exists(System.IO.Path
ChildFile.CopyTo(System.IO
End If
End If
Next
' copy all the sub-directories by recursively calling this same routine
Dim SubDir As DirectoryInfo
For Each SubDir In SourceDir.GetDirectories()
CopyDirectory(SubDir.FullN
Next
Else
Throw New DirectoryNotFoundException
End If
End Sub
HTH
Andy
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
Thogek and heintalus have provided valuable input but bramsquad's solution is the quickest and more robust. Therefore bramsquad gets the 500 points.
Thanks to all three for helping out.
Thogek and heintalus have provided valuable input but bramsquad's solution is the quickest and more robust. Therefore bramsquad gets the 500 points.
Thanks to all three for helping out.
Perhaps something like:
astrFileNames = Directory.GetFiles(DirSour
For i = 0 to astrFileNames.Length
File.Copy(strOldDirectoryP
Next
Assuming that strOldDirectoryPath and strNewDirectoryPath end in a backslash. If not, then
File.Copy(strOldDirectoryP
might work better.