-2
對於我的工作,我需要比較2個網絡文件夾。我需要將丟失的子文件夾的名稱輸出到.txt文件。通過比較2個文件夾在子文件夾中的區別
例子:
Folder1
-Sub1
-Sub2
-Sub4
Folder2
-Sub1
-Sub3
-Sub4
輸出:
Missing Sub2, Sub3
我怎樣才能做到這一點?
對於我的工作,我需要比較2個網絡文件夾。我需要將丟失的子文件夾的名稱輸出到.txt文件。通過比較2個文件夾在子文件夾中的區別
例子:
Folder1
-Sub1
-Sub2
-Sub4
Folder2
-Sub1
-Sub3
-Sub4
輸出:
Missing Sub2, Sub3
我怎樣才能做到這一點?
下面我寫了抓取每個文件夾的子目錄的代碼。然後通過這些目錄循環獲取文件夾的名稱。它從每個文件夾中找到丟失的子目錄,並將它們收集到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()
看看Directory.GetDirectories:https://msdn.microsoft.com/en-us/library/c1sez4sc(v=vs.110).aspx。您可以比較並提取丟失的目錄,並使用StreamWriter將它們輸出到文件中:https://msdn.microsoft.com/en-us/library/system.io.streamwriter(v=vs.110).aspx –
示例在這兩個鏈接應該可以幫助你實現你需要的東西 –
你有沒有嘗試過任何東西?我們不會爲您完成完整的編碼。查看[DirectoryInfo](https://msdn.microsoft.com/en-us/library/system.io.directoryinfo(v = vs.110).aspx)類和[Directory.GetDirectories()](https: //msdn.microsoft.com/en-us/library/ms143325(v=vs.110).aspx)方法,請閱讀[LINQ](https://msdn.microsoft.com/de-de/library/bb763068 .aspx),尤其是[Intersection](https://msdn.microsoft.com/en-us/library/bb460136(v = vs.110).aspx),如果遇到具體問題,請返回。 –