|
-
Mar 20th, 2007, 08:31 PM
#1
Thread Starter
New Member
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.
-
Mar 20th, 2007, 08:43 PM
#2
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.
-
Mar 20th, 2007, 09:33 PM
#3
Thread Starter
New Member
Re: Copying all files and subdirectories in a directory
Hey, the UIOption overload actually fixed my problem! Thank you so much!
-
Mar 21st, 2007, 09:06 AM
#4
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]
-
Mar 21st, 2007, 12:44 PM
#5
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|