Solved

Copying all files in a directory to another directory

Posted on 2004-10-04
4
223 Views
Last Modified: 2010-04-23
Hi,

I'd like to know how to copy all files (*.*) in directory DirSource to a directory DirTarget.

I tried hard with Directory.GetFiles(DirSource).CopyTo ... but coudn't do it.

This is a beginner's question, sorry.

Any help shall be welcome.
0
Comment
Question by:Ghanisen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 15

Expert Comment

by:Thogek
ID: 12219755
Note that Directory.GetFiles(DirSource) returns an array of strings containing the names of files in the specified directory.  (http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemiodirectoryclassgetfilestopic1.asp)  At this point, it's just an array of strings....

Perhaps something like:

    astrFileNames = Directory.GetFiles(DirSource)
    For i = 0 to astrFileNames.Length
        File.Copy(strOldDirectoryPath & astrFileNames(i), strNewDirectoryPath & astrFileNames(i))
    Next

Assuming that strOldDirectoryPath and strNewDirectoryPath end in a backslash.  If not, then

        File.Copy(strOldDirectoryPath & "\" & astrFileNames(i), strNewDirectoryPath & "\" & astrFileNames(i))

might work better.
0
 
LVL 10

Expert Comment

by:heintalus
ID: 12219883
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("Destination 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.Combine(DestDir.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(DestDir.FullName, ChildFile.Name)) Then
                        ChildFile.CopyTo(System.IO.Path.Combine(DestDir.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.FullName, System.IO.Path.Combine(DestDir.FullName, SubDir.Name), Overwrite)
            Next
        Else
            Throw New DirectoryNotFoundException("Source directory does not exist: " + SourceDir.FullName)
        End If
    End Sub

HTH
Andy
0
 
LVL 8

Accepted Solution

by:
bramsquad earned 500 total points
ID: 12219906
   Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim DirSource As String = "D:\"
        Dim DirTarget As String = "F:\"

        Dim dir As New System.IO.DirectoryInfo(DirSource)
        Dim file As System.IO.FileInfo

        For Each file In dir.GetFiles
            file.CopyTo(DirTarget + file.Name)
        Next
    End Sub
0
 

Author Comment

by:Ghanisen
ID: 12221173
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question