Check if directory empty OR no of files in directory.

  • Thread starter Thread starter Michael Beckinsale
  • Start date Start date
M

Michael Beckinsale

Hi All,

I am in the middle of writing code for a spreadsheet solution and need to
check whether a specific directory is empty or not.

Can anybody tell me what the code is to do that please. ?

Alternatively does anybody know the code to count the number of files in a
directory ? I could then apply an IF >0 type statement to achieve the same
result.

All suggestions gratefully received.

Regards

Michael beckinsale
 
use the filesystemobject
Set fso = CreateObject("Scripting.FileSystemObject")
of the top of my head, I believe there is a files object
so fso.files.count > 0

check the filesystemobject under help and you will see lots of examples
Paul
 
Sub CountFiles()
Dim iCount As Integer
Dim x As Integer
Dim strPath As String

strPath = "C:\ParentFolder\TargetFolder\"
x = Len(Dir(strPath))
If x <> 0 Then
Do
iCount = iCount + 1
x = Len(Dir)
Loop Until x = 0
MsgBox "There are " & iCount & " file(s)."
Else
MsgBox "There are no files."
End If
End Sub
 
Back
Top