简体   繁体   中英

Copy certain directories to another directory

I need some assistence in the following task. I have a very large directory structure like below:

g:\documents\100
g:\documents\100\100001
g:\documents\100\100001\Incoming
g:\documents\100\100001\Workfiles
g:\documents\100\100001\Customer Files
g:\documents\100\100002
g:\documents\100\100002\Incoming
g:\documents\100\100002\Workfiles
g:\documents\100\100002\Customer Files
...
g:\documents\100\100999
g:\documents\100\100999\Incoming
g:\documents\100\100999\Workfiles
g:\documents\100\100999\Customer Files

...

g:\documents\101
g:\documents\101\101001
g:\documents\101\101001\Incoming
g:\documents\101\101001\Workfiles
g:\documents\101\101001\Customer Files
g:\documents\101\101002
g:\documents\101\101002\Incoming
g:\documents\101\101002\Workfiles
g:\documents\101\101002\Customer Files
...
g:\documents\101\101999
g:\documents\101\101999\Incoming
g:\documents\101\101999\Workfiles
g:\documents\101\101999\Customer Files

etc...

I want to move alle directory's Incoming and Workfiles into a new path eq: z:\\documents with the same structure.

How can I iterate throug all the directory's and move the complete directory with the name is equal to Incoming or WorkFiles and create a structure that is the same to the old structure.

Resulting in:

g:\documents\100
g:\documents\100\100001
g:\documents\100\100001\Customer Files
g:\documents\100\100002
g:\documents\100\100002\Customer Files
...
g:\documents\100\100999
g:\documents\100\100999\Customer Files

z:\documents\100\100001\Incoming
z:\documents\100\100001\Workfiles
z:\documents\100\100002\Incoming
z:\documents\100\100002\Workfiles
...
z:\documents\100\100999\Incoming
z:\documents\100\100999\Workfiles

I want to do this in VB.NET or is there a syntax for robocopy that can do this job? other tools?

Thanks in advance!

You could do it by iterating the directory structure, and saving the paths that match WorkFiles & Incoming as the current found subdirectory. The other paths get scanned further, the matching ones are saved to process at a later stage.

After you got all matches, you then move the directories one by one to their new location. As the directories maintain their full path, it's easy enough to simply do a replace sourceDirectory with targetDirectory to get the new path. Then you check, if the parent path of this new path already exists, when not create it, and then move the directory from the source folder to the target folder

As an example, i wrote this one in Vb.net:

Public Class MoveDirectories
    Protected ReadOnly ListToMove As IList(Of String) = New List(Of String)

    Public Function GetMatching(ByVal sourceDirectory As String) As IList(Of String)
        Dim lst As New List(Of String)
        Dim subFolders() As String = Directory.GetDirectories(sourceDirectory)

        For Each subFolder As String In subFolders
            Dim folderName As String = subFolder.Substring(subFolder.LastIndexOf("\") + 1).ToLower()
            If ListToMove.Contains(folderName) Then
                lst.Add(subFolder)
                Continue For
            End If
            Dim subFolderMatching = GetMatching(subFolder)
            If subFolderMatching IsNot Nothing AndAlso subFolderMatching.Count > 0 Then
                For Each sfm As String In subFolderMatching
                    lst.Add(sfm)
                Next
            End If
        Next

        Return lst
    End Function

    Public Function ScanAndMoveMatching(ByVal sourceDirectory As String, ByVal targetDirectory As String) As Boolean
        Dim success As Boolean = True
        Dim matching As IList(Of String)

        Try
            matching = GetMatching(sourceDirectory)

            If Not Directory.Exists(targetDirectory) Then
                Directory.CreateDirectory(targetDirectory)
            End If

            For Each folder As String In matching
                Dim target As String = folder.Replace(sourceDirectory, targetDirectory)
                Dim subTarget As String = target.Substring(0, target.LastIndexOf("\"))
                If Not Directory.Exists(subTarget) Then
                    Directory.CreateDirectory(subTarget)
                End If
                Directory.Move(folder, target)
            Next

        Catch ex As Exception
            success = False
            Console.WriteLine("Error occured: {0}", ex.Message)
        End Try


        Return success
    End Function

    Public Sub New(ByVal ParamArray moveFolder As String())
        If (moveFolder IsNot Nothing) Then
            For i As Integer = 0 To moveFolder.Length - 1
                ListToMove.Add(moveFolder(i).ToLower())
            Next
        End If
    End Sub
End Class

which would then get created and called in your program in the following way:

Sub Main()
    Dim dScanner As New MoveDirectories("workfiles", "incoming")
    If dScanner.ScanAndMoveMatching("g:\documents", "z:\documents") Then
        Console.WriteLine("Succeeded!")
    Else
        Console.WriteLine("Failed!")
    End If
    Console.ReadLine()
End Sub

if you would rather see a list of matching items first, you could simply call the GetMatching method, that returns exactly that :)

Using robocopy:

robocopy "g:\documents\100" "z:\documents\100\" /E

/E is to copy subfolders, including empty ones.

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM