We help IT Professionals succeed at work.

New podcast episode! Our very own Community Manager, Rob Jurd, gives his insight on the value of an online community. Listen Now!

x

VBA Script to return Folder File and Folder count

ARampton
ARampton asked
on
293 Views
Last Modified: 2018-02-06
I need a reliable Access VBA script to show what Windows folder Property Contains: xxx Files, yyy Folders

My test script show the folder size total correctly but I can't find the equivalent of the total File and Folder counts

Some scripts I have seen seem to count immediate folder contents but don't count recursively through whole contents of target folder

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fsoFolder = fso.GetFolder(ProjectFolderPath)

    If fso.FolderExists(ProjectFolderPath) Then
        ProjectFolderNameCalculated = fsoFolder.Name
        ProjectFolderSizeCalculated = fsoFolder.Size
'        ProjectFolderFileCountCalculated = fsoFolder.Files.Count
'        ProjectFolderCountCalculated = fsoFolder.Folders.Count
     End If

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
Hi,

pls try
filePath = "C:\Test\"
filesCnt = UBound(Split(CreateObject("WScript.Shell").Exec("CMD /C DIR """ & filePath & """ /S /B /A:-D").StdOut.ReadAll, vbCrLf)) - 1
foldersCnt = UBound(Split(CreateObject("WScript.Shell").Exec("CMD /C DIR """ & filePath & """ /S /B /A:D").StdOut.ReadAll, vbCrLf)) - 1

Open in new window

Regards
ARamptonIT Manager

Author

Commented:
Interesting use of cmd dialog

This does count whole folder contents but its folder and file counts are one short of what folder property shows
CERTIFIED EXPERT
Top Expert 2016

Commented:
the minus one is not necessary since the last line is empty and ubound is zero-based
filePath = "C:\Test\"
filesCnt = UBound(Split(CreateObject("WScript.Shell").Exec("CMD /C DIR """ & filePath & """ /S /B /A:-D").StdOut.ReadAll, vbCrLf)) 
foldersCnt = UBound(Split(CreateObject("WScript.Shell").Exec("CMD /C DIR """ & filePath & """ /S /B /A:D").StdOut.ReadAll, vbCrLf)) 

Open in new window

ARamptonIT Manager

Author

Commented:
Is there a way of doing this without using cmd and its dialog visibility?
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
So you want the count of all folders and files under a specified base folder, including nested subfolders and files?

~bp
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
then try run
Sub Macro9()
Set objFSO = CreateObject("Scripting.FileSystemObject")
filePath = "C:\Test\"
Set shll = CreateObject("WScript.Shell")
shll.Run "CMD /C DIR """ & filePath & """ /S /B /A:-D >C:\Temp\Test.txt", 0, True
Set objTF = objFSO.OpenTextFile("C:\temp\test.txt", 1)
strIn = objTF.ReadAll
filesCnt = UBound(Split(strIn, vbCrLf))
objTF.Close
shll.Run "CMD /C DIR """ & filePath & """ /S /B /A:D >C:\Temp\Test.txt", 0, True
Set objTF = objFSO.OpenTextFile("C:\temp\test.txt", 1)
strIn = objTF.ReadAll
foldersCnt = UBound(Split(strIn, vbCrLf))
objTF.Close
Set objTF = Nothing
Set objFSO = Nothing
Set shll = Nothing
End Sub

Open in new window

ARamptonIT Manager

Author

Commented:
Both solutions work well thanks
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.