Results 1 to 5 of 5

Thread: Copying all files and subdirectories in a directory

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    5

    Smile Copying all files and subdirectories in a directory

    Hi guys.

    I'm writing a program for my work that is supposed to copy a directory from any location (Including our server machine) to another directory specified elsewhere.

    I can get it to work with small directories using My.Computer.FileSystem.CopyDirectory(), but this seems to time out and cause some COM threading error in VB that talks about how long operations can time out or something when I am debugging it on our 2GB directories.

    I'm actually kinda new to VB, so I was wondering if anyone has any code snippets or ideas on how to speed up and stabilize the process of copying all the files and subdirectories of a directory into another directory. The goal is to copy large directories of files and subdirectories efficiently and as stable and quick as possible. I am using Visual Basic 2005.
    My code is as follows, again, very simple, and not very stable with large directories.

    Try
    My.Computer.FileSystem.CopyDirectory(QASharePath, StableSharePath, True)
    Catch ex As Exception
    MsgBox("QA To Stable: Unable to complete the operation. Please check the paths and try again.")
    End Try

    Thank you for any suggestions or help you can give me. Happy Coding!
    Last edited by gholin; Mar 20th, 2007 at 08:35 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Copying all files and subdirectories in a directory

    Try using one of the overloads of CopyDirectory that takes a UIOption value and displays a progress dialogue. See if it still times out on big folders.

    You could also create your own recursive method to copy each file and folder individually using IO.Directory.CreateDirectory and IO.File.Copy.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    5

    Re: Copying all files and subdirectories in a directory

    Hey, the UIOption overload actually fixed my problem! Thank you so much!

  4. #4
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Copying all files and subdirectories in a directory

    could always do it yourself too:
    [vbcode]
    Private Sub CopyDirectory(ByVal Src As String, ByVal Dest As String)
    'add Directory Seperator Character (\) for the string concatenation shown later
    If Dest.Substring(Dest.Length - 1, 1) <> Path.DirectorySeparatorChar Then Dest += Path.DirectorySeparatorChar
    'If Directory.Exists(Dest) = False Then Directory.CreateDirectory(Dest)
    Dim Files() As String = Directory.GetFileSystemEntries(Src)
    For Each element As String In Files
    If Directory.Exists(element) = True Then
    'if the current FileSystemEntry is a directory, call this function recursively
    mintNumFolders += 1
    Directory.CreateDirectory(Dest & Path.GetFileName(element))
    CopyDirectory(element, Dest & Path.GetFileName(element))
    Else
    'the current FileSystemEntry is a file so just copy/move it
    mintNumFiles += 1
    If mblnMoveFiles = True Then
    If File.Exists(Dest & Path.GetFileName(element)) = True Then File.Delete(Dest & Path.GetFileName(element))
    File.Move(element, Dest & Path.GetFileName(element))
    Else 'Copy it
    File.Copy(element, Dest & Path.GetFileName(element), mblnOverWriteFiles)
    End If
    End If
    Next element
    End Sub
    [/vbcode]
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    5

    Re: Copying all files and subdirectories in a directory

    Hey JuggaloBrotha,
    thank you for taking the time to show me some code to do this. I will definitely digest this and learn from it so I can do things more myself rather than relying on kludges or overloads. Thanks again to both of you guys. My program is working pretty good now and I'm learning a lot about how better to use VBasic thanks to your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width