Results 1 to 13 of 13

Thread: [RESOLVED] Got dialog now what? /Final goal: folder contents 2 Excel SO Resolved

Threaded View

  1. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Got dialog now what? zaza knows /Final goal: folder contents 2 Excel

    So, I'm really bored today....

    Here's a revised version that is not limitted to those 4 file properties. In this version you can pass an array of properites to the function and get the values for each.

    Again, I have a sample proc that shows how to use the function.
    VB Code:
    1. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    2. ' Comments:     Sample Proc showing how to use the FolderDetails
    3. '               function
    4. '
    5. ' Arguments:    None
    6. '
    7. ' Date          Developer       Action
    8. ' --------------------------------------------------------------
    9. ' 03/23/06      Declan Kenny    Initial version
    10. '
    11. Sub GetFileDetails()
    12. Dim sFolder As String
    13. Dim vFolderDetail As Variant
    14. Dim lRecord As Long
    15. Dim lField As Long
    16.    
    17.     'Get the folder to document
    18.     sFolder = GetFolder
    19.    
    20.     'Only continue if a folder has been chosen
    21.     If sFolder = "" Then
    22.         MsgBox "No Folder Selected", vbExclamation
    23.     Else
    24.         'Call the function to return the list of
    25.         'file properties
    26.         vFolderDetail = FolderDetails(sFolder, True, Array("Name", "Type", "DATELASTMODIFIED", "Size", "Path", "ShortPath"), True)
    27.     End If
    28.    
    29.     'Output the results to a sheet in the current book
    30.     For lRecord = LBound(vFolderDetail, 2) To UBound(vFolderDetail, 2)
    31.         For lField = LBound(vFolderDetail) To UBound(vFolderDetail)
    32.             ActiveWorkbook.Worksheets(3).Cells(lRecord + 1, lField + 1) = vFolderDetail(lField, lRecord)
    33.         Next lField
    34.     Next lRecord
    35. End Sub
    36.  
    37. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    38. ' Comments:     This function returns the full path to a single
    39. '               folder chosen by the user
    40. '
    41. ' Arguments:    None
    42. '
    43. ' Date          Developer       Action
    44. ' --------------------------------------------------------------
    45. ' 03/23/06      Declan Kenny    Initial version
    46. '
    47. Function GetFolder() As String
    48. Dim dlgOpen As FileDialog
    49. Dim sFolder As String
    50.    
    51.     Set dlgOpen = Application.FileDialog( _
    52.         FileDialogType:=msoFileDialogFolderPicker)
    53.    
    54.     With dlgOpen
    55.         .AllowMultiSelect = False
    56.         If .Show = -1 Then
    57.             sFolder = dlgOpen.SelectedItems(1)
    58.         End If
    59.     End With
    60.    
    61.     GetFolder = sFolder
    62. End Function
    63.  
    64.  
    65. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    66. ' Comments:     This function returns an array of properties for
    67. '               all files in a folder and, optionaly, it sub-folders.
    68. '               This function will be called recursively if
    69. '               sub-folders are included.
    70. '
    71. ' Arguments:    FolderPath          The full path to the folder
    72. '                                   being documented.
    73. '
    74. '               IncludeSubFolders   Boolean to indicate if
    75. '                                   sub-folders should be included
    76. '
    77. '               FileProps           An array of file properties to
    78. '                                   be returned
    79. '
    80. '               FirstCall           A boolean used to indicate that
    81. '                                   the function is being called
    82. '                                   originally or recursively
    83. '
    84. ' Date          Developer       Action
    85. ' --------------------------------------------------------------
    86. ' 03/23/06      Declan Kenny    Initial version
    87. '
    88. Function FolderDetails(ByVal FolderPath As String, _
    89.                         ByVal IncludeSubFolders As Boolean, _
    90.                         ByRef FileProps As Variant, _
    91.                         Optional ByVal FirstCall As Boolean = True) As Variant
    92.  
    93. Dim fsoFileSys As Scripting.FileSystemObject
    94. Dim oFolder As Scripting.Folder
    95. Dim oFile As Scripting.File
    96. Dim oSubFolder As Scripting.Folder
    97. Dim vReturn As Variant
    98. Dim vSubReturn As Variant
    99. Dim lPropNum As Long
    100. Dim sPropValue As String
    101. Dim lSubRecord As Long
    102.    
    103.     'Create file system objects
    104.     Set fsoFileSys = New Scripting.FileSystemObject
    105.     Set oFolder = fsoFileSys.GetFolder(FolderPath)
    106.    
    107.     'Set the initial size of the return array
    108.     ReDim vReturn(LBound(FileProps) To UBound(FileProps), 0)
    109.    
    110.     'Add the Property headers to the return array
    111.     'Only if they have not yet been added
    112.     If FirstCall Then
    113.         For lPropNum = LBound(FileProps) To UBound(FileProps)
    114.             vReturn(lPropNum, 0) = FileProps(lPropNum)
    115.         Next lPropNum
    116.     End If
    117.  
    118.     'Loop through each file in the folder
    119.     'adding its properties to the array
    120.     For Each oFile In oFolder.Files
    121.         'Increase the size of the array by one
    122.         ReDim Preserve vReturn(LBound(FileProps) To UBound(FileProps), UBound(vReturn, 2) + 1)
    123.        
    124.         'Loop through the properties, adding each to the return array
    125.         For lPropNum = LBound(FileProps) To UBound(FileProps)
    126.            
    127.             'Determine which file property to add
    128.             Select Case UCase(FileProps(lPropNum))
    129.                 Case "DATECREATED":         sPropValue = oFile.DateCreated
    130.                 Case "DATELASTACCESSED":    sPropValue = oFile.DateLastAccessed
    131.                 Case "DATELASTMODIFIED":    sPropValue = oFile.DateLastModified
    132.                 Case "DRIVE":               sPropValue = oFile.Drive
    133.                 Case "NAME":                sPropValue = oFile.Name
    134.                 Case "PARENTFOLDER":        sPropValue = oFile.ParentFolder
    135.                 Case "PATH":                sPropValue = Replace(oFile.Path, oFile.Name, "")
    136.                 Case "SHORTNAME":           sPropValue = oFile.ShortName
    137.                 Case "SHORTPATH":           sPropValue = oFile.ShortPath
    138.                 Case "SIZE":                sPropValue = oFile.Size
    139.                 Case "TYPE":                sPropValue = oFile.Type
    140.                 Case Else:                  sPropValue = ""
    141.             End Select
    142.            
    143.             'Add the propert to the array
    144.             vReturn(lPropNum, UBound(vReturn, 2)) = sPropValue
    145.         Next lPropNum
    146.     Next oFile
    147.    
    148.     'Do sub-folders need to be included?
    149.     If IncludeSubFolders Then
    150.        
    151.         'loop through each sub-folder
    152.         For Each oSubFolder In oFolder.SubFolders
    153.            
    154.             'Recursive call to this function
    155.             vSubReturn = FolderDetails(oSubFolder.Path, True, FileProps, False)
    156.            
    157.             'Pass the values from the sub-call into the return array
    158.             For lSubRecord = 1 To UBound(vSubReturn, 2)
    159.                 ReDim Preserve vReturn(LBound(FileProps) To UBound(FileProps), UBound(vReturn, 2) + 1)
    160.                 For lPropNum = LBound(FileProps) To UBound(FileProps)
    161.                     vReturn(lPropNum, UBound(vReturn, 2)) = vSubReturn(lPropNum, lSubRecord)
    162.                 Next lPropNum
    163.             Next lSubRecord
    164.         Next oSubFolder
    165.     End If
    166.    
    167.     'Return the array
    168.     FolderDetails = vReturn
    169.    
    170.     'Clean up
    171.     Set oSubFolder = Nothing
    172.     Set oFile = Nothing
    173.     Set oFolder = Nothing
    174.     Set fsoFileSys = Nothing
    175. End Function
    Last edited by DKenny; Mar 23rd, 2006 at 03:12 PM.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

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



Click Here to Expand Forum to Full Width