1

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

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

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

任何帮助将不胜感激。

4

2 回答 2

0

我以前写过这个,应该服务器作为你版本的基础。我知道它不是 .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
于 2013-08-16T14:10:43.537 回答