How to quickly count the number of files in a folder

countfilesystemswindows-server-2003

On a Windows 2003 Server I have an application that processes requests and stores them in a folder as a queue where a second process comes and processes these stored requests. However at times the number of files tend to reach 100,000.

Using Windows Explorer or even dir at the command prompt doesn't seem to be much of a solution for me to find out how many files there are stored. It also takes too much time, CPU and I/O.

Any suggestions?

Best Answer

I'm not sure how fast this will work on a folder that big, but it's worth trying.

Save the code below in a .vbs file. Then, run it from a CMD prompt like this:

CSCRIPT FileCount.vbs C:\Insert\Your\Path\Here.

It only counts the file in the root of the path, but it could be modified to include sub-folders.

Dim oArgs
Set oArgs = WScript.Arguments

If oArgs.Count >= 1 Then
   WScript.Echo FileCount(oArgs(0))   
End If

'------------------------------------------------------------
'
'------------------------------------------------------------
Function FileCount(sPath)
   Dim oFSO
   Dim oFolder
   Dim oFiles

   Set oFSO = CreateObject("Scripting.FileSystemObject")
   Set oFolder = oFSO.GetFolder(sPath)
   Set oFiles = oFolder.Files
   FileCount = oFiles.Count
End Function