By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,956 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,956 IT Pros & Developers. It's quick & easy.

Using Dir() to list folder names

100+
P: 175
I'm using Dir() which is built into VB6, in order to go to a folder and find the names of all the files in that folder. It's extremely simple.

BUT, it won't tell me what folders are in the folder. I've searched the internet for a very long time, and found something about putting vbDirectory as the second argument, but it's not making a difference.

The only other thing I saw was "FindFileFirst", but I can't seem to get that to work at all.

So, my question is, how do I get the names of folders using "Dir()"?

( I'm using Dir() because I don't know the ENTIRE name of the files / folders. )
Apr 24 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 5K+
P: 8,434
I'm using Dir() which is built into VB6, in order to go to a folder and find the names of all the files in that folder. It's extremely simple.

BUT, it won't tell me what folders are in the folder. I've searched the internet for a very long time, and found something about putting vbDirectory as the second argument, but it's not making a difference.

The only other thing I saw was "FindFileFirst", but I can't seem to get that to work at all.

So, my question is, how do I get the names of folders using "Dir()"?

( I'm using Dir() because I don't know the ENTIRE name of the files / folders. )
I think the problem is that the vbDirectory parameter tells Dir function to also return directories. So you will be getting both directories and files. There may be better ways (see below) but you can use GetAttr() function to check the attributes of each name returned, and see whether it's a directory.

Try pasting this code into a new form...
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Click()
  2.   Dim A As String
  3.   A = Dir$("C:\*.*", vbDirectory)
  4.   Do While A <> ""
  5.     Debug.Print A, GetAttr("C:\" & A)
  6.     A = Dir$
  7.   Loop
  8. End Sub
One important thing to keep in mind is that Dir returns just the name, without the path. (See how I had to add path "C:\" again to use GetAttr?) This can mess things up if you don't allow for it.

In fact, I think you'd be well-advised to check out the FileSystemObject object. There are probably three main considerations when comparing it to VB's built-in folder and file handling.
  • It's slightly more complex to work with.
  • It requires you to add Microsoft Scripting runtime in Project references.
  • It provides much more functionality to your code.
If you do a search on TheScripts (see the search box at top right) you'll find plenty of help on how to use it.
Apr 25 '07 #2

Expert 5K+
P: 8,434
P.S. In case you haven't dealt with "bitwise comparisons" before, to check whether a name returned by Dir() function is a directory, just pass it (including full path if it's not in your current directory) to this sample function I built (but haven't tested, so be warned)...
Expand|Select|Wrap|Line Numbers
  1. Public Function IsADirectory(ByVal TheName As String) As Boolean
  2.   If GetAttr(TheName) And vbDirectory Then
  3.     IsADirectory = True
  4.   End If
  5. End Function
If you need to know more about bitwise comparisons (the And is the significant part here) look up GetAttr() in the doco.

So, to modify our original code, you could do something like this...
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Click()
  2.   Dim A As String
  3.   A = Dir$("C:\*.*", vbDirectory)
  4.   Do While A <> ""
  5.     Debug.Print A, 
  6.     If IsADirectory("C:\" & A) Then
  7.       Debug.Print , "<--- This is a directory!"
  8.     Else
  9.       Debug.Print ' Trust me, you should leave this line here.
  10.     End If
  11.     A = Dir$
  12.   Loop
  13. End Sub
Apr 25 '07 #3

P: 1
Another way to check whether it is a file name or a folder name is :

Fld = Dir$("C:\path\", vbDirectory)
If GetAttr("C:\path\" & Fld) = 16 Then
MsgBox("This is a folder")
End If

I tried it, works perfectly.

Best
Engin
Nov 3 '11 #4

Expert 5K+
P: 8,434
Watch out for direct comparisons like that. The various bits in the attribute byte mean different things. If the "16 bit" is set indicating it's a folder, there could also be another attribute set, so you wouldn't see exactly 16. That's why I used the And comparison, to test a specific bit.

Check the documentation for GetAttr, I think it explains this.
Dec 13 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.