我需要在以下任务中提供一些帮助。我有一个非常大的目录结构,如下所示:
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...
我想将alle目录的Incoming和Workfiles移动到具有相同结构的新路径e.q。:z:\ documents。
如何遍历所有目录并移动名称等于Incoming或WorkFiles的完整目录,并创建一个与旧结构相同的结构。
导致:
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
我想在VB.NET中执行此操作,或者是否有可以执行此工作的robocopy语法? 其他工具?
提前致谢!
答案 0 :(得分:1)
您可以通过迭代目录结构并保存与WorkFiles和amp;匹配的路径来实现。作为当前找到的子目录传入。其他路径进一步扫描,匹配的路径将保存到稍后阶段。
获得所有匹配后,然后将目录逐个移动到新位置。由于目录保持完整路径,因此只需使用targetDirectory替换sourceDirectory来获取新路径就足够了。然后检查,如果此新路径的父路径已存在,何时不创建它,然后将目录从源文件夹移动到目标文件夹
作为一个例子,我在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
然后以下列方式在程序中创建和调用:
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
如果您希望首先查看匹配项的列表,则可以简单地调用GetMatching方法,该方法将返回以下内容:)
答案 1 :(得分:0)
使用robocopy:
robocopy "g:\documents\100" "z:\documents\100\" /E
/ E是复制子文件夹,包括空文件夹。
参考: