Results 1 to 2 of 2

Thread: Copy files from one directory excluding a specific folder

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2018

    Copy files from one directory excluding a specific folder

    Hi guys, I'm trying to make a solution where I can copy over all files/folders from one directory to another but excluding a specific folder ("data"). Here's what I have so far

    Dim Paths As New ArrayList
    For Each Item As String In Paths
         If Not Item.ToString.Contains("\data\") Then
            My.Computer.FileSystem.CopyDirectory(Item, "D:\path1\path2\path3", True)
                End If
    Dim Files As New ArrayList
    For Each Item As String In Paths
        My.Computer.FileSystem.CopyFile(Item, "D:\path1\path2\path3", True)
    The theory behind this is to add all directories and folders to the array list, and if one of the items contains the data folder, then don't copy it. The result I'm getting is that it copies all the files within the folders of Directory1, and places them into Directory2 without excluding the data folder OR copying the root files in Directory1. Can anyone point out where I'm going wrong?
    Last edited by BobTarkish; Mar 8th, 2018 at 04:48 AM. Reason: Code indent format

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    South Louisiana

    Re: Copy files from one directory excluding a specific folder

    I haven't tested it, but something like this should work:
    Dim directory As IO.DirectoryInfo
    Dim file As IO.FileInfo
    For Each item As IO.FileSystemInfo In New IO.DirectoryInfo("C:\Users\path1\path2").GetFileSystemInfos().Where(Function(info) Not info.Name.Equals("data", StringComparison.OrdinalIgnoreCase)).ToArray
        If item.Attributes = IO.FileAttributes.Directory Then
            directory = New IO.DirectoryInfo(item.FullName)
            file = New IO.FileInfo(item.FullName)
        End If
    Essentially what it does is it gets every file and directory from C:\Users\path1\path2 where the name is not data and then iterates through the collection. Inside the iteration it converts the FileSystemInfo to its respective type and then moves the directory/file to D:\path1\path2\path3.

    The reason that I use the LINQ query is because the searchPattern parameter in the new constructor for IO.DirectoryInfo does not accept Regular Expressions. If you were wanting to get all files/directories that match a pattern rather than NOT match a pattern, we could just pass the search query in the new constructor.

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