Copying Directories/Files faster

vbcoop

Member
Joined
Oct 1, 2008
Messages
8
Programming Experience
Beginner
Hi

Does anyone know of a better quick way to copy directories/files in VB.net than using
VB.NET:
Expand Collapse Copy
Computer.FileSystem.CopyFile

The reason is because I’m developing a system that copies files/directories from one or multiple location to one location, to do this I use the below function

VB.NET:
Expand Collapse Copy
Public Sub copyDirectory(ByVal strtoROOTlocation As String, ByVal strrootfromName As String, ByVal root As DirectoryInfo, ByVal intIndex As Integer, ByVal video As Boolean)


        Dim strfromDirectoryFullPath As String = root.FullName

        Dim strtoDrectoryName As String
        Dim blcopy As Boolean
        If intIndex = 0 Then
            strtoDrectoryName = ""
        Else
            strtoDrectoryName = "\" + root.Name
        End If
        
            For Each fiifiles As FileInfo In root.GetFiles


                If blcopy Then

                    'This will stop Un needed files being copied
                    If Not fiifiles.Extension = ".doc" And Not fiifiles.Extension = ".fla" And Not fiifiles.Extension = ".db" Then

                       My.Computer.FileSystem.CopyFile(strfromDirectoryFullPath + "\" + fiifiles.Name, strtoROOTlocation + "\" + strtoDrectoryName + "\" + fiifiles.Name, True)
                        'frmmain.build.BackgroundWorker1.ReportProgress("********" + fiifiles.Name, "full")

                    Else
                    End If
                End If 'END IF (blcopy Second Level)
            Next ' END Root Folder - Content Loop
        Else
        End If 'END IF (NO video)
        For Each driirectory As DirectoryInfo In root.GetDirectories
            copyDirectory(strtoROOTlocation + "\" + strtoDrectoryName, strtoROOTlocation + "\" + strtoDrectoryName, driirectory, intIndex + 1, video)

        Next ' END Root Folder - Content Loop
    End Sub
This seems fine until it starts copying large files…

Does anyone know of a better way that I could increase the speed of copying large files..

Thanks
 
oof.. there's a couple of problems in there..

copyDirectory 'please get the naming conventions right: CopyDirectory
strtoROOTlocation 'naming conventions: strToRootLocation
strrootfromName 'naming conventions
video as Boolean 'it is recommended that Boolean names imply true or false: isVideo, wasVideo, canConvert

strfromDirectoryFullPath 'naming conventions
strtoDrectoryName 'naming conventions, spelling
blcopy 'naming conventions
strtoDrectoryName = "\" + root.Name 'use Path.Combine() to concatenate paths. DO NOT USE + TO CONCATENATE STRINGS
fiifiles 'naming conventions

If Not fiifiles.Extension = ".doc" And Not fiifiles.Extension = ".fla" And Not fiifiles.Extension = ".db" Then 'avoid big control blocks. this is better:
If f.Extension = ".doc" OrElse f.Extension = ".fla" OrElse f.Extension = ".db" Then Continue

My.Computer.FileSystem.CopyFile(...) 'it would be neater and make your code more self documenting if you'd make named variables for these strings first
'frmmain.build.BackgroundWorker1.ReportProgress("********" + fiifiles.Name, "full") 'no overload of ReportProgress takes string, string as arguments. rework the parameters
Elsex2 'empty else blocks; omit the Else

driirectory ' spelling?

-

as to your actual question.. if you want CopyFile to copy faster, buy a more high performance hard disk and do not copy files between folders on the same physical disk
 
Back
Top