Folder empty script

Emmanuel

Master Tech
Local time
Today, 09:11
Joined
Sep 4, 2002
Messages
88
Hi guys,

I have the code below that a friend gave me, but I need to change it to check multiple folders. Can you please provide me with some help? I need to do something like an elseif so it can check folders:

"\\tbstp\R2W\Admin\Watch"
"\\tbstp\R2W\Admin\Watch2"
"\\tbstp\R2W\Admin\Watch3"


'************* Code starts ******************
Option Explicit

Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")

Function FolderEmpty(strFolderPathName)
Dim oFiles, oFile, oFolder, oSubFolders, oSubFolder
Dim blnFileFound : blnFileFound = False
Set oFolder = oFS.GetFolder(strFolderPathName)
Set oFiles = oFolder.Files
If oFiles.Count > 0 Then
FolderEmpty = False
Exit Function
End If
Set oSubFolders = oFolder.SubFolders
For Each oSubFolder In oSubFolders
If Not FolderEmpty(oSubFolder.Path) Then
FolderEmpty = False
Exit Function
End If
Next
FolderEmpty = True
End Function

Dim strFolderPathName : strFolderPathName = "\\tbstp\R2W\Admin\Watch"
If not FolderEmpty(strFolderPathName) Then
MsgBox "Directory " & strFolderPathName & " is Empty."
Else
MsgBox "Directory " & strFolderPathName & " has files."
End If

'************** Code ends *************************


Thanks,
WebManny
 
What problem are you currently experiencing?
 
I don't think the code needs altering at all - it's a function - just call it as many times as you need it.

Questions:
Is it just those three folders you're checking, or is it an indeterminate quantity of numbered folders?

What result do you want if:
-None of your folders are empty
-One of your folders is empty
-All of your folders are empty

?
 
I don't think the code needs altering at all - it's a function - just call it as many times as you need it.

Questions:
Is it just those three folders you're checking, or is it an indeterminate quantity of numbered folders?

What result do you want if:
-None of your folders are empty
-One of your folders is empty
-All of your folders are empty

?

I appretiate the explanation, but could you provide me with an example of how to call tha function many times? Also, your examples of the results is exactly what I'm looking for. I may pull some more folders, but I just want to see which ones had files inside of them.
 
If the folders are all numbered in a sequence like that, then something like this might work:

Code:
Dim intLoop as Integer
Dim strFolderPathName as String
Dim strAnswer as string

For intLoop = 1 to 3
strFolderPathName = "\\tbstp\R2W\Admin\Watch"
if intLoop >1 then strFolderPathName = strFolderPathName & trim(str(intLoop))

If not FolderEmpty(strFolderPathName) Then
strAnswer = strAnswer &  "Directory " & strFolderPathName & " is Empty." & vbcrlf
Else
strAnswer = strAnswer &   "Directory " & strFolderPathName & " has files." & vbcrlf
End If

next intLoop
msgbox strAnswer


If the list of folders isn't going to be consistently named like that, you'll have to store a list of names in a table or array, then loop through that, reading the folder and checking it
 
Thanks. I'll ive this a try. If you have some free time, could you show me the example of the other scenario where the file names are not sequentially numbered like above? I really appretiate you taking the time to reply. Thanks!
 
No problem - it's going to be something like this:

Code:
Dim intLoop as Integer
Dim strFolderPathNames(1 To 3) as String
Dim strAnswer as string

intLoop(1) = "\\tbstp\R2W\Admin\Watch"
intLoop(2) = "\\tbstp\R2W\Admin\Banana"
intLoop(3) = "\\tbstp\R2W\Admin\SomethingElse"

For intLoop = 1 to 3

If not FolderEmpty(strFolderPathName(intLoop)) Then
strAnswer = strAnswer &  "Directory " & strFolderPathName(intLoop) & " is Empty." & vbcrlf
Else
strAnswer = strAnswer &   "Directory " & strFolderPathName(intLoop) & " has files." & vbcrlf
End If

next intLoop
msgbox strAnswer
 
Thanks again. I may be doing something wrong. I get the error screen attached.

This is the code modofied with actual folders:
Dim intLoop as Integer
Dim strFolderPathNames (1 to 3) as String
Dim strAnswer as string

intLoop(1) = "\\tbstp\R2W\Admin\Watch"
intLoop(2) = "\\tbstp\R2W\Admin\Work"
intLoop(3) = "\\tbstp\R2W\Admin\UnidentifiedReports"

For intLoop = 1 to 3

If not FolderEmpty(strFolderPathName(intLoop)) Then
strAnswer = strAnswer & "Directory " & strFolderPathName(intLoop) & " is Empty." & vbcrlf
Else
strAnswer = strAnswer & "Directory " & strFolderPathName(intLoop) & " has files." & vbcrlf
End If

next intLoop
msgbox strAnswer
 

Attachments

  • untitled.JPG
    untitled.JPG
    15.3 KB · Views: 214
I don't know - that looks as though it's falling over on the first declaration line - which I can't see anything wrong with...
 
Thanks anyways for checking and for sharing the code.
 

Users who are viewing this thread

Back
Top Bottom