Copy ALL Files and Folders Excluding Hidden or System Files

~OSD~ 2,131 Reputation points


Initially I was using following VB code to copy directory (with all files and sub-directories etc.)

Dim src_UsersFolder = "C:\myData\"
           Dim dst_UsersBackup = "D:\Backup\" 
           My.Computer.FileSystem.CopyDirectory(src_UsersFolder, dst_UsersBackup, overwrite:=True) 

But eventually I received error message(s) if any file or folder is protected /hidden /System Files etc.

Then I changed the design and used Windows 10 built-in command line tool XCOPY

xcopy C:\myData  D:\Backup /E /C /I /Y /Q /J

And excluded the /H flag (that copies hidden and system files also).

Unluckily, XCOPY didn't worked as it should be as "Insufficient Memory" error occurred.
Upon searching, I could found some suggestions to avoid this insufficient memory error like how-to-fix-out-of-memory-error but this didn't helped either.
Question is can we take help from VB.Net to exclude the system /hidden files and ignore the files /folder longer than 254 characters which seems to be Windows maximum path length.

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,755 questions
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,594 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,196 Reputation points

    Hello @~OSD~

    Have you considered something like a recursive method? The following was taken and slightly modified from here. One improvement would be to wrap the code in with asynchronous task if the folder is large and you want the front end to remain responsive.


    Imports System.IO  
    Public Class FileDirectoryOperations  
        Public Delegate Sub OnErrorDelegate(exception As Exception)  
        Public Shared Event OnErrorEvent As OnErrorDelegate  
        Public Shared Sub CopyFolder(sourcePath As String, destinationPath As String)  
            Dim sourceDirectoryInfo As New DirectoryInfo(sourcePath)  
            If (sourceDirectoryInfo.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden OrElse  
               (sourceDirectoryInfo.Attributes And FileAttributes.System) = FileAttributes.System OrElse  
               (sourceDirectoryInfo.Attributes And FileAttributes.ReparsePoint) = FileAttributes.ReparsePoint Then  
                Exit Sub  
                ' If the destination folder don't exist then create it  
                If Not Directory.Exists(destinationPath) Then  
                End If  
                Dim fileSystemInfo As FileSystemInfo  
                    For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos  
                        If (fileSystemInfo.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden OrElse  
                           (fileSystemInfo.Attributes And FileAttributes.System) = FileAttributes.System Then  
                            Continue For  
                            Dim destinationFileName As String = Path.Combine(destinationPath, fileSystemInfo.Name)  
                            If TypeOf fileSystemInfo Is FileInfo Then  
                                File.Copy(fileSystemInfo.FullName, destinationFileName, True)  
                                CopyFolder(fileSystemInfo.FullName, destinationFileName)  
                            End If  
                        End If  
                Catch ex As Exception  
                    RaiseEvent OnErrorEvent(ex)  
                End Try  
            End If  
        End Sub  
    End Class  

    Event for errors in form

    Handle the error as you see fit

    Private Sub OnCopyError(exception As Exception)  
    End Sub  

    Add this in form load or shown event

    AddHandler FileDirectoryOperations.OnErrorEvent, AddressOf OnCopyError  


    FileDirectoryOperations.CopyFolder("C:\myData", "D:\Backup")  
    1 person found this answer helpful.
    0 comments No comments

  2. ~OSD~ 2,131 Reputation points

    Thanks for input karen, will check this out.

    Will it "skip" or "ignore" the long file names and paths (longer than 254 characters)?