我有一个递归搜索所有文件和子文件夹,但我想在另一个目录中创建确切的文件夹结构。
非常感谢你的帮助,我试图自己做这个以及在网上看,但我还没有找到任何东西。所以我希望ConvertDir中的文件夹结构在SaveDir位置重新创建,没有任何文件。我也希望在找到它们的同时创建它们,但我不知道这是否可行或明智。
这将创建在SaveDir顶层找到的文件夹,而不是在正确的位置。
这是我的代码的副本:
On Error Resume Next
Dim ObjFolder
Dim ObjSubFolders
Dim ObjSubFolder
Dim ObjFiles
Dim ObjFile
Dim objFileSecuritySettings
Dim intRetVal
Dim objSD
Dim objFolderSecuritySettings
ObjFolder = FSO.GetFolder(FolderName)
ObjFiles = ObjFolder.Files
For Each ObjFile In ObjFiles 'Write all files to output files
objFileSecuritySettings = _
objWMIService.Get("Win32_LogicalFileSecuritySetting='" & ObjFile.Path & "'")
intRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD)
If intRetVal = 0 Then
ObjOutFile.WriteLine(ObjFile.Path) ' write in CSV format
End If
Next
ObjSubFolders = ObjFolder.SubFolders 'Getting all subfolders
For Each ObjFolder In ObjSubFolders
objFolderSecuritySettings = _
objWMIService.Get("Win32_LogicalFileSecuritySetting='" & ObjFile.Path & "'")
intRetVal = objFolderSecuritySettings.GetSecurityDescriptor(objSD)
Directory.CreateDirectory(SaveDir + "\\" + ObjFolder.name)
If intRetVal = 0 Then
ObjOutFile.WriteLine(ObjFolder.Path) ' write in CSV format
ObjOutFile.WriteLine(ObjFolder.ObjSubFolders)
End If
Gather(ObjFolder.Path)
Next
提前谢谢。
AntonSK
答案 0 :(得分:1)
您可以让方法在它启动的根文件夹上传递,以保持目录树的完整性。 并使用它:
ReCreateDirectoryStructure("C:\somefolder\", "D:\")
Private Sub ReCreateDirectoryStructure(ByVal sourceDir As String, _
ByVal targetDir As String, Optional ByVal rootDir As String = "")
If rootDir = String.Empty Then
rootDir = sourceDir
End If
Dim folders() As String = IO.Directory.GetDirectories(sourceDir)
For Each folder As String In folders
Directory.CreateDirectory(folder.Replace(rootDir, targetDir))
ReCreateDirectoryStructure(folder, targetDir, rootDir)
Next
End Sub