Results 1 to 2 of 2

Thread: Copy files from one directory excluding a specific folder

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    1

    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

    Code:
    Dim Paths As New ArrayList
    Paths.AddRange(Directory.GetDirectories("C:\Users\path1\path2"))
    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
            Next
                
    Dim Files As New ArrayList
    Files.AddRange(Directory.GetFiles(("C:\Users\path1\path2"))
    For Each Item As String In Paths
        My.Computer.FileSystem.CopyFile(Item, "D:\path1\path2\path3", True)
    Next
    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
    Location
    South Louisiana
    Posts
    11,737

    Re: Copy files from one directory excluding a specific folder

    I haven't tested it, but something like this should work:
    Code:
    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)
            directory.MoveTo("D:\path1\path2\path3")
        Else
            file = New IO.FileInfo(item.FullName)
            file.MoveTo("D:\path1\path2\path3")
        End If
    Next
    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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