Hi Everyone,
I am trying to do the following.
I have a folder e.g. C:\Folder1
I have this path defined as folderPath1.
I want to run a macro that will check that Folder1 has 24 files in it. If
not, a message box will display, prompting the user to check the folder again.
Can anyone help?
Thanks
Dave
You can use the FileSystemObject.
The code below sets a reference to Microsoft Scripting Runtime. You could use
late binding but if you set a reference, it's easier to get Help while writing
the routines.
As written, the count returned includes all files, including any that might be
Hidden or various System Files.
But you could also use the FSO to test the files to see if they should be
counted based on their attributes and type.
==================================
Option Explicit
Sub filecount()
'Requires setting reference to Microsoft Scripting Runtime
Dim fso As FileSystemObject
Dim fo As Folder
Dim fi As File
Dim i As Long
'change this to reflect your actual path
Const fldname As String = "C:\"
Set fso = New FileSystemObject
Set fo = fso.getfolder(fldname)
i = fo.Files.Count
If i <> 24 Then
MsgBox ("Folder has " & i & " files. Should have 24")
End If
'Below was just used for debugging
'might be useful if you want to filter the count based on file type
'On Error GoTo errMgr 'some files gave an error looking at ShortName
'For Each fi In fo.Files
' i = i + 1
' Debug.Print fi.ShortName, fi.Name, , fi.Attributes, fi.Type
'Next fi
'Exit Sub
'errMgr:
' Debug.Print "**Error**" & vbLf & fi.Name, , fi.Attributes, fi.Type & vbLf
'Resume Next
End Sub
==========================================
--ron