Results 1 to 5 of 5

Thread: File Lister

  1. #1
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    File Lister

    I'm looking for a VB script that will list every file in every folder and all subfolders. The user will need to be
    able to indicate an entire drive ("C:") or any folder on the drive. I've seen <post#=165842>post 165842</post#>, but
    that code only lists the files and folders in 1 parent folder.

    I'd use Dir /s, but I need more info than dir returns.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: File Lister

    Assuming you plan to dump each file name (and whatever other info you need) directly to a text output of some kind, sounds as though you would want to use the FileSystemObject to start at the appropriate folder and get at its contents, and the Dictionary object to store subfolders for later exploration. There are security issues with the FileSystemObject - I assume this is an Intranet Zone application?

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: File Lister

    Your assumptions are, of course, correct. I needed the push you provided by mentioning the Dictionary object to store the subfolders. I was stuck there. Thanks, Jefferson.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  4. #4
    Star Lounger
    Join Date
    Jan 2001
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: File Lister

    Actually, you don
    Attached Files Attached Files

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: File Lister

    You are quite right! I got pushed in that direction after a google search. Here's what I came up with:


    Dim oFso As Object
    Dim oFso2 As Object
    Dim oShellFolder As Object
    Dim oFileOut As Object
    Public Count As Integer
    Public Size As Long

    Sub FileLister()
    Dim strTitle As String
    Dim strOutFile As String
    Dim strFolder As String

    strTitle = "Select a Folder: "
    strOutFile = "C:FileList.csv"

    'Create output file for stream
    Set oFso2 = CreateObject("Scripting.FileSystemObject")
    Set oFileOut = oFso2.CreateTextFile(strOutFile, True)

    'Create the navigable object
    Set oFso = CreateObject("Scripting.FileSystemObject")

    Set oShellFolder = CreateObject("Shell.Application").BrowseForFolder( &H0, strTitle, &H1)
    strFolder = oShellFolder.Items.Item.Path
    Set oShellFolder = Nothing

    'Print column headings
    oFileOut.Write "!FileName" & "," & "!FileFullPath" & "," & "!FileType" & ","
    oFileOut.WriteLine "!Last Accessed Date" & "," & "!Last Modified Date" & "," & "!Created Date" & "," & "!Attributes"

    List (strFolder)

    Debug.Print CStr(Count) & " files. Total space occupied: " & CStr(Size)
    oFileOut.WriteBlankLines (1)
    oFileOut.WriteLine CStr(Count) & " files. Total bytes: " & CStr(Size)
    oFileOut.Close
    Set oFso = Nothing
    Set oFso2 = Nothing

    End Sub

    Function List(sFolder As String)
    Dim oFile As File
    Dim oFolder As Folder
    Dim oFolders As Folders

    Dim aFolder As Folder

    Set oFolder = oFso.GetFolder(sFolder)
    ListFiles (sFolder)
    Set oFolders = oFolder.SubFolders

    For Each aFolder In oFolders
    Debug.Print aFolder.Path & "," & aFolder.DateLastModified & "," & aFolder.DateLastModified
    oFileOut.Write aFolder.Name & "," & aFolder.Path & "," & aFolder.Type & ","
    oFileOut.Write aFolder.DateLastAccessed & "," & aFolder.DateLastModified & "," & aFolder.DateCreated & ","
    oFileOut.WriteLine aFolder.Attributes
    List (aFolder.Path)
    Next

    End Function

    Sub ListFiles(sFolder As String)
    Dim oFile As File
    Dim oFiles As Files
    Dim oFolder As Folder

    Set oFolder = oFso.GetFolder(sFolder)
    Set oFiles = oFolder.Files

    For Each oFile In oFiles
    Debug.Print oFile.Path & "," & oFile.DateLastModified & "," & oFile.DateLastModified
    oFileOut.Write oFile.Name & "," & oFile.Path & "," & oFile.Type & ","
    oFileOut.Write oFile.DateLastAccessed & "," & oFile.DateLastModified & "," & oFile.DateCreated & ","
    oFileOut.WriteLine oFile.Attributes
    Count = Count + 1
    Size = Size + oFile.Size
    Next
    End Sub
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •