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
 
Hi I keep getting the same error on the line StrFle2 = Dir
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Make sure the right StrFle is linked to the right StrDir : 1 with 1, 2 with 2, etc...

Code:
StrFle1 = Dir(StrDir1)
StrFle2 = Dir(StrDir2)

And you're testing the right StrFle

Code:
Do While Len(StrFle1)
StrFle1 = Dir
Loop

I had the same error, I corrected this, and it worked.
If it works for me it will work for you.
 
Upvote 0
Yes I have doubled checked that.

So when I get the error, the value of the StrFle2 there was the pdf file in that folder since there was only one pdf file there.

I can't figure out what the issue is for now.
 
Upvote 0
You should send me the part of your code so I can help you figure out what the problem is
 
Upvote 0
This is the code:

Code:
Sub test ()
StrDir1 = ThisWorkbook.Path & "\PDF_FILES\" & "\" & Sheet10.Range("B17").Text & "\" & Sheet10.Range("C12").Text & "\" & "\folder1\"

StrDir2 = ThisWorkbook.Path & "\PDF_FILES\" & "\" & Sheet10.Range("B17").Text & "\" & Sheet10.Range("C12").Text & "\" & "\folder2\"

StrFle1 = Dir (StrDir1)
StrFle2 = Dir (StrDir2)

i = 0
j = 0

Do While Len(StrDir1)>0
i = i + 1
StrDir1 = Dir 
Loop

Do While Len(StrDir2)>0
j  = j + 1
StrDir2 = Dir 
Loop

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

If j = 0 Then 
       MsgBox "The folder is Empty"
Else
       MsgBox "There is " & j  & " files in the folder"
End If 
End Sub
 
Upvote 0
The problem comes from your paths. Both have 2 backslash "" back to back which causes an error.

Try with these paths :

Code:
StrDir1 = ThisWorkbook.Path & "\PDF_FILES\" & Sheet10.Range("B17").Text & "\" & Sheet10.Range("C12").Text & "\folder1\"

StrDir2 = ThisWorkbook.Path & "\PDF_FILES\" & Sheet10.Range("B17").Text & "\" & Sheet10.Range("C12").Text & "\folder2\"

Also i don't know what Sheet10 refers to but you have to declare it first.

Code:
Dim Sheet10 As Worksheet
Set Sheet10 = Worksheets("Whatever name")

I also just noticed you are doing :

Code:
StrDir1 = Dir

Instead of :

Code:
StrFle1 = Dir

You have to use "Dir" on StrFle1, not StrDir1
 
Last edited:
Upvote 0
Change with this :

Code:
Do While Len(StrFle1)>0
i = i + 1
StrFle1 = Dir 
Loop

Do While Len(StrFle2)>0
j  = j + 1
StrFle2 = Dir 
Loop
 
Upvote 0
In have corrected the issue with the backslash. The others are correct here. I did those mistakes because I have to type the code again since I am on phone now.

But I think I am figuring out what the issue might be:

When I try to test the paths separately, that error on did not come.

The code was able to get to the folder and showed number of files in it.

Yet when I combined them , the error came again. I think the code is trying to read the files in the file it came across. Because it's still seeing that file inside there as a folder and since that file isn't a folder, then the error appears.

One thing: both folders are found at the same place. Can this be the issue?

Thanks
Kelly
 
Upvote 0
Where should I placertainly the second code?

You've probably moved beyond this, but you'd just put it within a sub, along with the code you're wanting to execute. The function is separate and can go at the top of your module.
 
Upvote 0
You've probably moved beyond this, but you'd just put it within a sub, along with the code you're wanting to execute. The function is separate and can go at the top of your module.

Okay I have been able to create an additional function and then set two paths and called then in the module and it has worked.

Is there a way to avoid the separate functions and make them one?

If yes then I will like you show me how to get three of such functions in one. Thanks
 
Upvote 0

Forum statistics

Threads
1,215,949
Messages
6,127,888
Members
449,411
Latest member
AppellatePerson

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