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
LVL 1
anthonytrAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rey Obrero (Capricorn1)Commented:
try this


 
	dim objFile as string
	
	Set objFiles = fso.GetFolder(strFolderName).Files
    
    'lngFileCount = objFiles.Count
	
        For Each objFile In objFiles
			if mid(objFile.path,InStrRev(objFile.Path, ".") + 1)= "PDF" _
				or mid(objFile.path,InStrRev(objFile.Path, ".") + 1)= "TIF" then

                lngFileCount = lngFileCount + 1
            End If
        Next objFile
    
    MsgBox lngFileCount     'Total number of files
    Me.number_of_items = lngFileCount


    Set fsoFolder = Nothing
    Set fso = Nothing
    Me.Refresh
                                

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rgonzo1971Commented:
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
anthonytrAuthor Commented:
Hi,

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

For Each objFile In objFiles

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Rey Obrero (Capricorn1)Commented:
oops sorry

change this
dim objFile as string
to

dim objFile as object
anthonytrAuthor Commented:
Hi Rey,

I missed that too!  Thanks your code works perfectly.

Anthony
anthonytrAuthor Commented:
Superb - thank you Rey
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.