recursive file.copy loop locks application

Posted on 2009-04-22
Last Modified: 2012-05-06
This loops works great to perform a recursive file copy, however, it locks up the form quite easily.
Any ideas of a better way to recursively copy files? If i put it in a class will that help?
' Usage: 

' Copy Recursive with Overwrite if exists. 

' RecursiveDirectoryCopy("C:\Data", "D:\Data", True, True) 

' Copy Recursive without Overwriting. 

' RecursiveDirectoryCopy("C:\Data", "D:\Data", True, False) 

' Copy this directory Only. Overwrite if exists. 

' RecursiveDirectoryCopy("C:\Data", "D:\Data", False, True) 

' Copy this directory only without overwriting. 

' RecursiveDirectoryCopy("C:\Data", "D:\Data", False, False) 

' Recursively copy all files and subdirectories from the specified source to the specified 

' destination. 

Private Sub RecursiveDirectoryCopy(ByVal sourceDir As String, ByVal destDir As String, ByVal fRecursive As Boolean, ByVal overWrite As Boolean) 

    Dim sDir As String 

    Dim dDirInfo As IO.DirectoryInfo 

    Dim sDirInfo As IO.DirectoryInfo 

    Dim sFile As String 

    Dim sFileInfo As IO.FileInfo 

    Dim dFileInfo As IO.FileInfo 

    ' Add trailing separators to the supplied paths if they don't exist. 

    If Not sourceDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then 

        sourceDir &= System.IO.Path.DirectorySeparatorChar 

    End If 

    If Not destDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then 

        destDir &= System.IO.Path.DirectorySeparatorChar 

    End If 

    'If destination directory does not exist, create it. 

    dDirInfo = New System.IO.DirectoryInfo(destDir) 

    If dDirInfo.Exists = False Then dDirInfo.Create() 

    dDirInfo = Nothing 

    ' Recursive switch to continue drilling down into directory structure. 

    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 


    End If 

    ' Get the files from the current parent. 

    For Each sFile In System.IO.Directory.GetFiles(sourceDir) 

        sFileInfo = New System.IO.FileInfo(sFile) 

        dFileInfo = New System.IO.FileInfo(Replace(sFile, sourceDir, destDir)) 

        'If File does not exist. Copy. 

        If dFileInfo.Exists = False Then 

            sFileInfo.CopyTo(dFileInfo.FullName, overWrite) 


            'If file exists and is the same length (size). Skip. 

            'If file exists and is of different Length (size) and overwrite = True. Copy 

            If sFileInfo.Length <> dFileInfo.Length AndAlso overWrite Then 

                sFileInfo.CopyTo(dFileInfo.FullName, overWrite) 

            'If file exists and is of different Length (size) and overwrite = False. Skip 

            ElseIf sFileInfo.Length <> dFileInfo.Length AndAlso Not overWrite Then 

                Debug.WriteLine(sFileInfo.FullName & " Not copied.") 

            End If 

        End If 

        sFileInfo = Nothing 

        dFileInfo = Nothing 


End Sub

Open in new window

Question by:scottsilvi

    Author Comment

    How can i add a progress bar for the file being copied?
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    You can alleviate the "lock" factor by placing a call to Application.DoEvents() inside each of your inner loops.  But this will only work up to a certain point because the CopyTo() call is actually a blocking call.  If you have a large file then it will just sit there until the copy is complete.

    You can't really add a progressbar to the existing code because with a recursive routine you don't actually know how many items you are dealing with!  For that to work you would have to make TWO complete to only count the files that will be copied, and another to actually copy the files.

    Author Comment

    is there a better way to copy files in then?
    Further i want a progress bar for the FILE being copied not the FILES.
    LVL 85

    Accepted Solution

    If you have VB.Net 2005 (or above) then there is an overloaded version of CopyFile() that allows you to specify that the default dialogs be shown during the copy operation:

        "AllDialogs - Show progress dialog box and any error dialog boxes."

    This would show the OS dialog window with the "flying folder animation" during the copy:

        My.Computer.FileSystem.CopyFile(sFileInfo.FullName, dFileInfo.FullName, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now