VB.Net - List files & subfolders from a specified Directory and save to a text document, and sort results

StackOverflow https://stackoverflow.com/questions/18274569

Domanda

I am working on a project that requires me to search and list all files in a folder that could have multiple sub folders and write it to text documents.

Primarily the file extension i will be searching for is a .Doc, but I will need to list the other files found in said directory as well.

To make things slightly more difficult I want the text documents to be sorted by File type and another by Directory. I do not know how possible this is, but I have search for methods online, but have as of yet found correct syntax.

Any help will be greatly appreciated.

Altri suggerimenti

I write this in the past, should server as a base for your version. I know it's not .NET, still I hope it helps something. It prompts the user for a path to scan, recurses into folders, and writes the file name, path, and owner into a CSV file. Probably really inefficient and slow, but does the job.

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top