Windows Folder Options Affecting Listing of Files (Shell.Application)

Devin

Board Regular
Joined
Jan 21, 2009
Messages
105
My goal is to get file sizes for XML files located in a ZIP folder. I have changed an Excel file’s extension to “.ZIP” and I am trying to only extract the .XML file names. FYI, this may only work on new versions of Windows and Excel. I am using Shell.Application to extract the file names. My problem is the full path—with the “.XML” extension—is only pulled when the folders settings are set to show the extensions (aka the folder options box “Hide extensions for known file types” must not be checked).

Is there a way to extract the file type (.XML) without having to make sure the folder’s settings are adjusted for this?

THANK YOU!

Code:
Sub GetSizes()
    Dim FName As String
    Dim MyPath, XMLZip As Variant
    Dim oApp As Object
    MyPath = Environ$("UserProfile") & "\Good.zip\xl\worksheets"
    
    FName = Dir(MyPath)
    Set oApp = CreateObject("Shell.Application")
    
    i = 3
    
    For Each XMLZip In oApp.Namespace(MyPath).Items
    
        If InStr(1, UCase(XMLZip), ".XML", vbTextCompare) > 0 Then
            Range("A" & i).Value = Left(XMLZip, Len(XMLZip) - 4)
            Range("B" & i).Value = XMLZip.Size / 1024
            i = i + 1
        End If
        
    Next
    
    Set oApp = Nothing
    
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I think I found a work around.

Code:
Sub GetSizes()
    Dim FName As String
    Dim MyPath, XMLZip As Variant
    Dim oApp As Object
    MyPath = Environ$("UserProfile") & "\Good.zip\xl\worksheets"
    
    FName = Dir(MyPath)
    Set oApp = CreateObject("Shell.Application")
    
    i = 3
    
    For Each XMLZip In oApp.Namespace(MyPath).Items
    
        If InStr(1, UCase(XMLZip), ".XML", vbTextCompare) > 0 Then
            Range("A" & i).Value = Left(XMLZip, Len(XMLZip) - 4)
            Range("B" & i).Value = XMLZip.Size / 1024
            i = i + 1

        Else

            If XMLZip.Type = "XML Document" Then
                Range("A" & i).Value = XMLZip
                Range("B" & i).Value = XMLZip.Size / 1024
                i = i + 1
            End If

        End If

    Next

    Set oApp = Nothing

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top