Link to home
Start Free TrialLog in
Avatar of anthonytr
anthonytrFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Count the number of Files in a folder

Hi,

I'm trying to get an accurate count of the number of files in a given folder, however at the moment it seems to be returning with a value which is +1.  I think this may be due to some hidden temp file in the folder or thumb. file .

My code currently is:

    Set objFiles = fso.GetFolder(strFolderName).Files
    
    lngFileCount = objFiles.Count
    
    MsgBox lngFileCount     'Total number of files
    Me.number_of_items = lngFileCount


    Set fsoFolder = Nothing
    Set fso = Nothing
    Me.Refresh

Open in new window


There will only ever be PDF, TIFF or (TIF) files stored in these folders, is there any way of only counting files with those extensions?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try

    Set objFiles = fso.GetFolder(strFolderName).Files
    lngCounter = 0
    For Each objFile In objFiles
        aName = Split(objFile.Name, ".")
        strExt = aName(UBound(aName))
        If UCase(strExt) = "PDF" Or _
                UCase(strExt) = "TIFF" Or _
                UCase(strExt) = "TIF" Then
            lngCounter = lngCounter + 1
        End If
    Next
    
    MsgBox lngCounter

Open in new window

Regards
Avatar of anthonytr

ASKER

Hi,

I get a 'Compile Error'  
For each control variable must be Variant or Object

For Each objFile In objFiles

Open in new window

oops sorry

change this
dim objFile as string
to

dim objFile as object
Hi Rey,

I missed that too!  Thanks your code works perfectly.

Anthony
Superb - thank you Rey