Consulting

Results 1 to 3 of 3

Thread: Solved: Listing file/folder attributes from user input

  1. #1

    Solved: Listing file/folder attributes from user input

    I have the following code which lists the attributes of the files held in a certain folder:-


    [VBA]
    Sub TestListFilesInFolder()
    Workbooks.Add ' create a new workbook for the file list
    ' add headers
    With Range("A1")
    .Formula = "Folder contents:"
    .Font.Bold = True
    .Font.Size = 12
    End With
    Range("A3").Formula = "File Name:"
    Range("B3").Formula = "File Size:"
    Range("C3").Formula = "File Type:"
    Range("D3").Formula = "Date Created:"
    Range("E3").Formula = "Date Last Accessed:"
    Range("F3").Formula = "Date Last Modified:"
    Range("A3:F3").Font.Bold = True
    ListFilesInFolder "'folder name here'", True 'Change this to your directory ' list all files included subfolders
    End Sub

    Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
    ' lists information about the files in SourceFolder
    ' example: ListFilesInFolder "C:\FolderName", True
    Dim FSO As Scripting.FileSystemObject
    Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder
    Dim FileItem As Scripting.File
    Dim r As Long
    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)
    r = Range("A65536").End(xlUp).Row + 1
    For Each FileItem In SourceFolder.Files
    ' display file properties
    Cells(r, 1).Formula = FileItem.Path
    Cells(r, 2).Formula = FileItem.Size
    Cells(r, 3).Formula = FileItem.Type
    Cells(r, 4).Formula = FileItem.DateCreated
    Cells(r, 5).Formula = FileItem.DateLastAccessed
    Cells(r, 6).Formula = FileItem.DateLastModified

    ' use file methods (not proper in this example)
    ' FileItem.Copy "C:\FolderName\Filename.txt", True
    ' FileItem.Move "C:\FolderName\Filename.txt"
    ' FileItem.Delete True
    r = r + 1 ' next row number
    Next FileItem
    If IncludeSubfolders Then
    For Each SubFolder In SourceFolder.SubFolders
    ListFilesInFolder SubFolder.Path, True
    Next SubFolder
    End If
    Columns("A:H").AutoFit
    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing
    ActiveWorkbook.Saved = True
    End Sub
    [/VBA]
    This works great, however, I would need to change the folder path every time for each folder.

    Is there a way of inserting some code to prompt the user to confirm the folder path?

    TIA

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Look at GetOpenFilename in VBA help.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Kelly,
    When you post code, please format it using the green VBA button
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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