Originally Posted by shrivallabha
Hi Binar,
I found some time today evening. I spent some time digging around Shell object's size property but it fails to return any info for Folder objects so it was of no use. Ultimately I have used FSO for that bit only. Along with FSO, formatting has added some overhead. Which means the code will be on the slower side.
Replace the previous code with this one.
[vba]'----------------------------------------------------------------------------------------------------------------------
'If you are copying and changing this code then do not forget to add:
'Tools | References | Microsoft Shell Controls and Automation
'----------------------------------------------------------------------------------------------------------------------
Option Explicit
Option Compare Text 'We might come across mixtures of uppercase lowercase letters sometimes
Public objShApp As Shell
Public i As Long
Public Sub RunFileFolderList()
Dim strPath As String
'----------------------------------------------------------------------------------------------------------------------
'Setting the worksheet to list results from row 11 and performing cleanup to remove previous listings
'----------------------------------------------------------------------------------------------------------------------
i = 11
If Range("A" & Rows.Count).End(xlUp).Row > i Then
Range("A11:A" & Range("A" & Rows.Count).End(xlUp).Row).EntireRow.Delete
End If
With Application
.ScreenUpdating = False
ListItemsInFolder Range("A9").Value, Range("B9").Value
.ScreenUpdating = True
End With
GetSizeAndFileInfo
Set objShApp = Nothing
End Sub
Public Sub ListItemsInFolder(strPath As String, boolSubFolder As Boolean)
Dim fldItem As ShellFolderItem
If objShApp Is Nothing Then Set objShApp = New Shell
'----------------------------------------------------------------------------------------------------------------------
'Shell's Namespace object holds onto many different and useful properties that can used to extract information
'In this code we have used its FileSystemObject equivalents
'----------------------------------------------------------------------------------------------------------------------
With objShApp.Namespace(strPath)
For Each fldItem In .Items
'----------------------------------------------------------------------------------------------------------------------
'The code tends to error when it comes across a zip file which in turn may contain a folder. The code then gives you
'an RTE so to bypass this possibility we use following check of verifying .zip
'----------------------------------------------------------------------------------------------------------------------
If InStr(fldItem.Parent, ".zip") = 0 Then
If fldItem.IsFolder Then
Cells(i, 1).Value = fldItem.Path
ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 3), Address:=fldItem.Path, TextToDisplay:="Click Here"
Cells(i, 1).Resize(, 5).Interior.ColorIndex = 48
i = i + 1
Else
Cells(i, 1).Value = Left(fldItem.Path, InStrRev(fldItem.Path, fldItem.Name) - 2)
Cells(i, 2).Value = fldItem.Name
ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 3), Address:=fldItem.Path, TextToDisplay:="Click Here"
'---------------------------------------------------------------------------------------------------------
'Binar, add columns here like I have demonstrated below
'---------------------------------------------------------------------------------------------------------
Cells(i, 5).Value = .GetDetailsOf(.ParseName(fldItem.Name), 27)
i = i + 1
End If
If fldItem.IsFolder And boolSubFolder Then ListItemsInFolder fldItem.Path, boolSubFolder
End If
Next fldItem
End With
End Sub
Private Sub GetSizeAndFileInfo()
Dim objFSO As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
For i = 11 To Range("A" & Rows.Count).End(xlUp).Row
If Len(Cells(i, 2).Value) = 0 Then
Cells(i, 4).Value = objFSO.GetFolder(Cells(i, 1).Value).Files.Count & " Files, " & _
Round(objFSO.GetFolder(Cells(i, 1).Value).Size / 1024 / 1024, 2) & " MB"
End If
Next i
Set objFSO = Nothing
End Sub
[/vba]