Sorting VB.Net-列出指定目录中的文件和子文件夹,并保存到文本文档中,然后对结果进行排序

Sorting VB.Net-列出指定目录中的文件和子文件夹,并保存到文本文档中,然后对结果进行排序,sorting,recursion,vb.net-2010,writetofile,Sorting,Recursion,Vb.net 2010,Writetofile,我正在从事一个项目,该项目要求我搜索并列出一个文件夹中可能有多个子文件夹的所有文件,并将其写入文本文档 我将搜索的文件扩展名主要是.Doc,但我还需要列出在该目录中找到的其他文件 为了让事情稍微复杂一点,我希望文本文档按文件类型排序,另一个按目录排序。 我不知道这有多可能,但我已经在网上搜索了一些方法,但到目前为止还没有找到正确的语法 任何帮助都将不胜感激。这些文章将对您有所帮助 我以前写过这个,应该把服务器作为你的版本的基础。我知道它不是.NET,但我还是希望它能有所帮助。它提示用户输入要扫描

我正在从事一个项目,该项目要求我搜索并列出一个文件夹中可能有多个子文件夹的所有文件,并将其写入文本文档

我将搜索的文件扩展名主要是.Doc,但我还需要列出在该目录中找到的其他文件

为了让事情稍微复杂一点,我希望文本文档按文件类型排序,另一个按目录排序。 我不知道这有多可能,但我已经在网上搜索了一些方法,但到目前为止还没有找到正确的语法


任何帮助都将不胜感激。

这些文章将对您有所帮助


我以前写过这个,应该把服务器作为你的版本的基础。我知道它不是.NET,但我还是希望它能有所帮助。它提示用户输入要扫描的路径,递归到文件夹中,并将文件名、路径和所有者写入CSV文件。可能效率很低,速度也很慢,但能完成任务

Main() ' trickster yo

Dim rootFolder 'As String
Dim FSO 'As Object
Dim ObjOutFile
Dim objWMIService 'As Object

Sub Main()
    StartTime = Timer()
    If Wscript.Arguments.Count = 1 Then ' if path provided with the argument, use it.
        rootFolder = Wscript.Arguments.Item(0)
    Else
        rootFolder = InputBox("Give me the search path : ") ' if not, ask for it
    End If
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set ObjOutFile = FSO.CreateTextFile("OutputFiles.csv")
    Set objWMIService = GetObject("winmgmts:")


    ObjOutFile.WriteLine ("Path, Owner") ' set headers

    Gather (rootFolder)

    ObjOutFile.Close ' close the stream

    EndTime = Timer()
    MsgBox ("Done. (ran for " & FormatNumber(EndTime - StartTime, 2) & "s.)")
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function Gather(FolderName)
    On Error Resume Next

    Dim ObjFolder
    Dim ObjSubFolders
    Dim ObjSubFolder
    Dim ObjFiles
    Dim ObjFile

    Set ObjFolder = FSO.GetFolder(FolderName)
    Set ObjFiles = ObjFolder.Files

    For Each ObjFile In ObjFiles  'Write all files to output files
        Set objFileSecuritySettings = _
        objWMIService.Get("Win32_LogicalFileSecuritySetting='" & ObjFile.Path & "'")
        intRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD)
        If intRetVal = 0 Then
            owner = objSD.owner.Domain & "\" & objSD.owner.Name
            ObjOutFile.WriteLine (ObjFile.Path & ";" & owner) ' write in CSV format
        End If
    Next

    Set ObjSubFolders = ObjFolder.SubFolders     'Getting all subfolders


    For Each ObjFolder In ObjSubFolders
        Set objFolderSecuritySettings = _
        objWMIService.Get("Win32_LogicalFileSecuritySetting='" & ObjFile.Path & "'")
        intRetVal = objFolderSecuritySettings.GetSecurityDescriptor(objSD)
        If intRetVal = 0 Then
            owner = objSD.owner.Domain & "\" & objSD.owner.Name
            ObjOutFile.WriteLine (ObjFolder.Path & ";" & owner) ' write in CSV format
        End If
        Gather (ObjFolder.Path)
    Next
End Function

谢谢,我去看看