Recursively copy folders

aspfun

Active member
Joined
Feb 16, 2010
Messages
34
Programming Experience
5-10
I copied code from the link below. It works fine.

How do I recursively copy all files and directories? - vbCity - The .NET Developer Community

I modify code as below and want program working as two goals"
1) If the size of source folder and destination folder are the same, skip;
2) If the size of source folder and destination folder are not the same, only copy different files (my code will copy all files)

But, my code never work as expect. How to modify it?

If fRecursive Then
' Get a list of directories from the current parent.
For Each sDir In System.IO.Directory.GetDirectories(sourceDir)
sDirInfo = New System.IO.DirectoryInfo(sDir)
dDirInfo = New System.IO.DirectoryInfo(destDir & sDirInfo.Name)

' Create the directory if it does not exist.
If dDirInfo.Exists = False Then
dDirInfo.Create()
' Since we are in recursive mode, copy the children also
RecursiveDirectoryCopy(sDirInfo.FullName, dDirInfo.FullName, fRecursive, overWrite)
sDirInfo = Nothing
dDirInfo = Nothing
ElseIf dDirInfo.Exists = True AndAlso GetFolderSize(sourceDir) <> GetFolderSize(destDir) Then
' Since we are in recursive mode, copy the children also
RecursiveDirectoryCopy(sDirInfo.FullName, dDirInfo.FullName, fRecursive, False)
sDirInfo = Nothing
dDirInfo = Nothing[/COLOR]
End If
Next
End If
 
You are going to have to elaborate on "my code never work as expect" ... what doesn't work, what are you expecting it to do?

Have you debugged the code? Have you set a breakpoint and seen what the code is doing? Those are where you need to start.
 
Here try this way...

VB.NET:
Imports System.IO

Public Class Form1
    Dim array As New ArrayList
    Dim strFromFolder As String
    Dim strToFolder As String

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'Set Source Folder
        Dim openFolder As New FolderBrowserDialog
        openFolder.ShowDialog()
        If openFolder.SelectedPath = "" Then
            Exit Sub
        Else
            strFromFolder = openFolder.SelectedPath
        End If

        'Set Destination Folder
        openFolder = New FolderBrowserDialog
        openFolder.ShowDialog()
        If openFolder.SelectedPath = "" Then
            Exit Sub
        Else
            strToFolder = openFolder.SelectedPath
        End If

        Dim thread As New Threading.Thread(AddressOf GetFilesRecursive)
        thread.Start()
        Do Until thread.IsAlive = False
            Application.DoEvents()
        Loop

        'Iterate through ArrayList
        For Each path As String In array
            Dim _file As String = Mid(path, strFromFolder.Length + 1, Len(path))
            Dim _newPath As String = strToFolder & _file
            Dim _folder As String = strToFolder & Mid(_file, 1, _file.LastIndexOf("\"))

            'If folder doesn't exist then create it
            If Not Directory.Exists(_folder) Then
                Directory.CreateDirectory(_folder)
                File.Copy(path, _newPath)
            Else
                'Only copy files that don't exist
                If Not File.Exists(_newPath) Then
                    File.Copy(path, _newPath)
                End If
            End If
        Next
    End Sub

    Function GetFilesRecursive() As List(Of String)
        Dim stack As New Stack(Of String)
        stack.Push(strFromFolder)
        Do While (stack.Count > 0)
            Dim dir As String = stack.Pop
            Try
                array.AddRange(Directory.GetFiles(dir, "*.*"))
                Dim directoryName As String
                For Each directoryName In Directory.GetDirectories(dir)
                    stack.Push(directoryName)
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Loop
        Return Nothing
    End Function
End Class

You really only need to satisfy your second condition...

If the size of source folder and destination folder are not the same, only copy different files (my code will copy all files)
 
Back
Top