How to tell if a string is a folder?

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try this function

Code:
Function DirExists(sSDirectory As String) As Boolean
If Dir(sSDirectory, vbDirectory) <> "" Then DirExists = True
End Function
 
Upvote 0
Code:
Function IsFolder(r As String) As Boolean
If Dir(r, vbDirectory) <> "" Then IsFolder = (GetAttr(r) = 16)
End Function
 
Last edited:
Upvote 0
BTW, I added some error handling to make this more robust:
Function IsFolder(strName As String) As Boolean
IsFolder = False
On Error Resume Next
IsFolder = Dir(strName, vbDirectory) <> "" And GetAttr(strName) = 16
End Function

Works like a charm!

Cheers! :)
 
Upvote 0
One more thing...
I ran into some issues with the getattr() i.e. some folders returned 48 (vbDirectory (16) +vbArchive (32) = 48). So, after much annoyance, I ended up writing a decimal to binary converter (since dec2bin is not an available function in VBA) and doing a bit wise comparison using the mid function. If anyone know of a better method (ie simpler and just as robust), please reply to this thread.
Cheers
 
Upvote 0
You could do a bitwise AND like this:
Code:
Function IsFolder(strName As String) As Boolean
    IsFolder = False
    On Error Resume Next
    IsFolder = Dir(strName, vbDirectory) <> "" And (GetAttr(strName) And vbDirectory) <> 0
End Function
 
Upvote 0
Setting IsFolder to False at the beginning of your code to FALSE is unnecessary as setting the function to type Boolean automatically initiliazes to FALSE. I changed my code slightly after my original post, that should make the On Error Line unneeded either.
 
Upvote 0
Code:
Function IsFolder(r As String) As Boolean
If Dir(r, vbDirectory) <> "" Then IsFolder = (GetAttr(r) And vbDirectory)
End Function
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,753
Members
452,940
Latest member
rootytrip

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