How to show message box if a filepath or folder is not empty

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hello Geniuses,

I am stacked here. I need a script that will check my filepath or the destination folder to see if there are files there already.

Then display alert.

Thanks
Kelly
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
File count:
Code:
Function FileCountA(Path As String) As Long
Dim strTemp As String
    Dim lngCount As Long
    strTemp = Dir(Path & "*.*")
    Do While strTemp <> ""
        lngCount = lngCount + 1
        strTemp = Dir
     Loop
    FileCountA = lngCount
End Function

Check if empty:

Code:
Dim fileCountNum as Long
fileCountNum = FileCountA("Drive:\Path\to\Your\Files")

If fileCountNum > 1 then
msgbox "A total of: " & fileCountNum & " files were identified in the specified path."
else
msgbox "No files were found within the specified path."
end if
 
Last edited:
Upvote 0
Hi,

Code:
Sub test()
    
Dim i As Integer
Dim StrDir, StrFle As String
StrDir = "C:\YourFolder1\YourFolder2\"
StrFle = Dir(StrDir)
i = 0
Do While Len(StrFle) > 0
i = i + 1
StrFle = Dir
Loop
If i = 0 Then
    MsgBox "Folder is Empty"
Else
    MsgBox "There is " & i & " files in the folder"
End If
End Sub
 
Upvote 0
Hi,

Code:
Sub test()
    
Dim i As Integer
Dim StrDir, StrFle As String
StrDir = "C:\YourFolder1\YourFolder2\"
StrFle = Dir(StrDir)
i = 0
Do While Len(StrFle) > 0
i = i + 1
StrFle = Dir
Loop
If i = 0 Then
    MsgBox "Folder is Empty"
Else
    MsgBox "There is " & i & " files in the folder"
End If
End Sub


Thanks very much for this.

It worked out just fine.

Kelly
 
Upvote 0
File count:
Code:
Function FileCountA(Path As String) As Long
Dim strTemp As String
    Dim lngCount As Long
    strTemp = Dir(Path & "*.*")
    Do While strTemp <> ""
        lngCount = lngCount + 1
        strTemp = Dir
     Loop
    FileCountA = lngCount
End Function

Check if empty:

Code:
Dim fileCountNum as Long
fileCountNum = FileCountA("Drive:\Path\to\Your\Files")

If fileCountNum > 1 then
msgbox "A total of: " & fileCountNum & " files were identified in the specified path."
else
msgbox "No files were found within the specified path."
end if
Where should I placertainly the second code?
 
Upvote 0
@louisH,

I am having some challenge here:

I am using the for loop to create some pdf files into my folder.

And since new files are added at each step, after the first file is created , then I don't have access to the

If i = 0 Then

Part of the code again.

I know it's weird but I wish there is a way to bypass this.

He reason I am using the loop for the creation of the pdf files is because I have vlookup formula that updates the data on that sheet before I create the pdf of it.

Thanks
 
Upvote 0
I don't really understand what you are trying to achieve.
If I didn't answer your question after this post can you send me some parts of your code ?

You can put back i to 0 after you created your files just by doing


If you want to bybass the test "If i = 0" you can do this :

Code:
If [Your condition] Then Goto Skip

If i = 0 Then
    MsgBox "Folder is Empty"
Else
    MsgBox "There is " & i & " files in the folder"
End If

Skip :
 
Last edited:
Upvote 0
The first code worked; i was then not doing something cool enough. Thanks a lot
Kelly
 
Upvote 0
I don't really understand what you are trying to achieve.
If I didn't answer your question after this post can you send me some parts of your code ?

You can put back i to 0 after you created your files just by doing



If you want to bybass the test "If i = 0" you can do this :

Code:
If [Your condition] Then Goto Skip

If i = 0 Then
    MsgBox "Folder is Empty"
Else
    MsgBox "There is " & i & " files in the folder"
End If

Skip :

Hello,

How do I use nested loops in the do lOops?

I have set three separate file paths as

StrDir1
StrDir2
StrDir3

Then

StrFle1 = Dir(StrDir1)
....etc to the third one.


I have the feeling I am doing something wrongly.

But one thing is that all the StrFle1 to StrFle3 are equalling to Dir


So I had this error on line:

StrFle2 = Dir

"Invalid procedure call or argument "


Thanks
Kelly
 
Upvote 0
Hi,

This will individually check if StrDir1 and StrDir2 are empty :

Code:
Sub test()
    
    Dim i, j As Integer
    Dim StrDir1, StrDir2, StrFle1, SteFle2 As String
    StrDir1 = "C:\banc\test\"
    StrDir2 = "C:\banc\test2\"
    
    StrFle1 = Dir(StrDir1)
    StrFle2 = Dir(StrDir2)
    
    i = 0
    j = 0
    
    Do While Len(StrFle1) > 0
        i = i + 1
        StrFle1 = Dir
    Loop
    
    Do While Len(StrFle2) > 0
        j = j + 1
        StrFle2 = Dir
    Loop
    
    If i = 0 Then
        MsgBox "Folder 1 is Empty"
    Else
        MsgBox "There is " & i & " files in the folder 1"
    End If
    
    If j = 0 Then
        MsgBox "Folder 2 is Empty"
    Else
        MsgBox "There is " & j & " files in the folder 2"
    End If

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top