对于我的工作,我需要比较2个网络文件夹。我需要输出.txt文件中缺少的子文件夹的名称。
示例:
Folder1
-Sub1
-Sub2
-Sub4
Folder2
-Sub1
-Sub3
-Sub4
输出:
Missing Sub2, Sub3
我怎么能这样做?
答案 0 :(得分:0)
下面我编写了代码来抓取每个文件夹的子目录。然后它遍历这些目录以获取文件夹的名称。它从每个文件夹中找到丢失的子目录,并将它们收集到2个列表中。
我怀疑有一种比我在这里更简单的方法,但它有效。
'Declare lists for each folders
Dim folder1SubDirectories As New List(Of String)
Dim folder1SubNames As New List(Of String)
Dim folder2SubDirectories As New List(Of String)
Dim folder2SubNames As New List(Of String)
'Declare folders locations as variables
Dim folder1Location As String = "Folder1 path location"
Dim folder2Location As String = "Folder2 path location"
'add the subdirectories of each folder into the appropriate list
folder1SubDirectories.AddRange(Directory.GetDirectories(folder1Location))
'add the name of the subdirectory to a new list
For Each subfolder In folder1SubDirectories
Dim dirInfo As New System.IO.DirectoryInfo(subfolder)
folder1SubNames.Add(dirInfo.Name)
Next
'add the subdirectories of each folder into the appropriate list
folder2SubDirectories.AddRange(Directory.GetDirectories(folder2Location))
'add the names of the subddirectories into a new list
For Each subfolder In folder2SubDirectories
Dim dirInfo As New System.IO.DirectoryInfo(subfolder)
folder2SubNames.Add(dirInfo.Name)
Next
'The below LINQ finds the missing subdirectories and puts them in them in the list
Dim missingSubs = folder1SubNames.Union(folder2SubNames).Except(folder1SubNames.Intersect(folder2SubNames)).ToList()
'Now you can do whatever you need to do with the missing subdirectories
For Each missingSub In missingSubs
Console.WriteLine(missingSub)
Next
Console.ReadLine()