VB.NET - copy directories/files faster

Asked By Chris Cooper on 09-Oct-08 09:03 AM

Hi

 

Does anyone know of a better quick way to copy directories/files in VB.net than using

[code]

Computer.FileSystem.CopyFile

[/code]

 

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

 

[code]

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

[/code]

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

Vb.net Copy file one directory to another Directory

Binny ch replied to Chris Cooper on 09-Oct-08 09:15 AM

See this code:

Public Class Form1

    ' First of all lets load up the file listbox with some files from C:\Test that have the .csv extension.
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Return an array of file names that match our *.csv pattern.
        Dim files() As String
        files = Directory.GetFiles("C:\Test", "*.csv")

        ' Now we will loop through these files, adding ONLY the filename portion of the file found.
        ' Directory.GetFiles returns whole paths.

        Dim file As String
        For Each file In files
            lstFiles.Items.Add(Path.GetFileName(file))
        Next
    End Sub

    ' This sub procedure handles the clicking of our "Browse" button for selecting a path.
    ' By default we have made it open to our C:\ root drive and gave it a nice description telling the user what to do.

    Private Sub btnSelectPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectPath.Click
        FolderBrowserDialog1.SelectedPath = "C:\"
        FolderBrowserDialog1.Description = "Select A Destination Folder"

        ' Show the dialog and if the user chose OK, then the dialog will return a DialogResult of OK
        ' We can then set our textbox for the Path to the path they selected.
        If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtPath.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    ' Simple button to cancel the application
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Application.Exit()
    End Sub

    ' This click event is where a bulk of the work is done. It deterines first if there is a destination path
    ' specified, that the path then exists, gets the selected files from the listbox and determines if they exist
    ' then copies only those files found and specified from the multiple selection listbox.

    Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
        If Not String.IsNullOrEmpty(txtPath.Text) Then

            ' Here we test if the path they chose actually exists. This will return false on drives
            ' not readable or not accessible (like if you chose a DVD drive for instance).
            If Directory.Exists(txtPath.Text) Then
                Dim theSelectedFile As String

                ' Here is where we loop through the selected items in the listbox.
                For Each theSelectedFile In lstFiles.SelectedItems

                    ' We need to formulate the full path again to test if it exists and to copy the file.
                    If File.Exists("c:\Test\" & theSelectedFile) Then

                        ' Here we do a simple file.copy call where we specify the file we want and then
                        ' specify the destination where we want to go. We let the user know each file that was
                        ' copied as well.
                        File.Copy("c:\Test\" & theSelectedFile, txtPath.Text & "\" & theSelectedFile)
                        MessageBox.Show("Copied: " & theSelectedFile)
                    End If
                Next
            Else
                MessageBox.Show("Ok, that path doesn't exist or inacccessible, quit trying to trick me!")
            End If

        Else
            MessageBox.Show("Please select a destination path before copying files")
        End If
    End Sub
End Class

File.Copy and FileSystem.CopyFile

Chris Cooper replied to Binny ch on 09-Oct-08 09:26 AM
Is there a difference between File.Copy and FileSystem.CopyFile?

reply

Perry replied to Chris Cooper on 09-Oct-08 09:39 AM

Hi,

FileSystem.CopyFile can be use for copying bulk files inside the source directory while File.Copy can be use to copy only single file at a time. I think the program you have written is the best one because if you go for File.Copy it will take time in iterating the files while Filesystem.CoyFile can do it for you.

Let me know if you are expecting any other details.

Regards,

Megha

File.Copy
Chris Cooper replied to Perry on 09-Oct-08 09:45 AM
I've tried that function, but I’ve noticed that you have to create the directory before copying the files.  The directories and sub directories if needed to be created is created automatically if needed ..

But that anyway, do you know any other way of improving the speed when it comes to larger files being copied?
reply
Perry replied to Chris Cooper on 09-Oct-08 09:55 AM

Hi,

Fundamentaly speaking if you copy all the files by overwriting even some files are already existed then there is no major enhancement you can made. But yes there is the way to sync only required files means copy only those files which are actually required. Also suppose file XXX has been modified then just copy the difference at the destination and not whole file. This will improve the performance drastically when you need to copy file system which size is in GBs. We are using Rsync utility for this. You need to wrap the Rsync in C#.

see http://www.mail-archive.com/rsync@lists.samba.org/msg17502.html and http://www.kolosy.com/wordpress/  to get the brief overview for this.

Regards,

Megha

Copy Files
Chris Cooper replied to Chris Cooper on 09-Oct-08 10:52 AM
I've got a better way... (Well it seems to be at the momemnt)
Using the System Shell.

If you want to have a look at this there is a simple example at
http://www.freevbcode.com/ShowCode.asp?ID=499&NoBox=True

re
Web Star replied to Chris Cooper on 10-Oct-08 01:58 AM

use this

Public Function ShellFileCopy(src As String, dest As String, _
    Optional NoConfirm As Boolean = False) As Boolean
'PURPOSE: COPY FILES VIA SHELL API
'THIS DISPLAYS THE COPY PROGRESS DIALOG BOX
'PARAMETERS: src: Source File (FullPath)
            'dest: Destination File (FullPath)
            'NoConfirm (Optional): If set to
            'true, no confirmation box
            'is displayed when overwriting
            'existing files, and no
            'copy progress dialog box is
            'displayed
            'Returns (True if Successful, false otherwise)
'EXAMPLE:  
  'dim bSuccess as boolean
  'bSuccess = ShellFileCopy ("C:\MyFile.txt", "D:\MyFile.txt")
Dim WinType_SFO As SHFILEOPSTRUCT
Dim lRet As Long
Dim lflags As Long
lflags = FOF_ALLOWUNDO
If NoConfirm Then lflags = lflags & FOF_NOCONFIRMATION
With WinType_SFO
    .wFunc = FO_COPY
    .pFrom = src
    .pTo = dest
    .fFlags = lflags
End With
lRet = SHFileOperation(WinType_SFO)
ShellFileCopy = (lRet = 0)
End Function
copy directories/files faster
C_A P replied to Chris Cooper on 10-Oct-08 07:22 AM
  1. ''' <summary>
  2.     ''' Method for copying all the _fileNames
  3.     ''' in a specified directory
  4.     ''' </summary>
  5.     ''' <param name="origDir">Directory the files are in</param>
  6.     ''' <param name="destDir">Directory the files are being copied to</param>
  7.     Public Function RecursiveCopy(ByVal origDir As String, ByVal destDir As String) As Boolean
  8.         'get all the info about the original directory
  9.         Dim dirInfo As New DirectoryInfo(origDir)
  10.         'retrieve all the _fileNames in the original directory
  11.         Dim http://www.google.com/search?q=files+msdn.microsoft.com As FileInfo() = dirInfo.GetFiles(origDir)
  12.         'always use a try...catch to deal
  13.         'with any exceptions that may occur
  14.         Try
  15.             'loop through all the file names and copy them
  16.             For Each http://www.google.com/search?q=file+msdn.microsoft.com As String In System.IO.Directory.GetFiles(origDir)
  17.                 Dim origFile As New FileInfo(_fileName)
  18.                 Dim destFile As FileInfo = New System.IO.FileInfo(_fileName.Replace(origDir, destDir))
  19.                 'copy the file, use the OverWrite overload to overwrite
  20.                 'destination file if it exists
  21.                 System.IO.File.Copy(origFile.FullName, destFile.FullName, True)
  22.                 'TODO: If you dont want to remove the original
  23.                 '_fileNames comment this line out
  24.                 System.IO.File.Delete(origFile.FullName)
  25.                 _status = True
  26.             Next
  27.             _returnMessage = "All _fileNames in " + origDir + " copied successfully!"
  28.         Catch ex As Exception
  29.             _status = False
  30.             'handle any errors that may have occurred
  31.             _returnMessage = ex.Message
  32.         End Try
  33.         Return _status
  34.     End Function