473,378 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Using Dir() to list folder names

175 100+
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
4 79550
Killer42
8,435 Expert 8TB
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
Killer42
8,435 Expert 8TB
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
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
Killer42
8,435 Expert 8TB
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

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

Similar topics

3
by: Robert | last post by:
How can I put the contents of a dir list box into a document such as word or notepad?
5
by: Craig | last post by:
I get an error when compiling the website about the first Reports folder. If I change the name the website compiles. Am I doing something wrong?? Folder list...... CoolingTower Boilers...
11
by: Nir Aides | last post by:
Hello, Is there a solution or a work around for the sys.path problem with unicode folder names on Windows XP? I need to be able to import modules from a folder with a non-ascii name. ...
35
by: Shyguy | last post by:
Is it possible, and if so how would I read and import folder names from a CD to a table? Thank you for any help.
5
by: alokb | last post by:
Hello friends! I am working on vc++. I want to retrieve programatically the folder names of a mailbox of exchange server. Is there any way to retrieve these folders? I want to retrieve those...
4
by: Jay | last post by:
What's the recommended way to change file/folder names within a project? I want to change the solution name from (say) sol1.sln to sol2.sln, and also everything else named sol1.* (eg sol1.suo,...
2
by: Bill Fallon | last post by:
I have a VS2005 VB.Net windows form application deployed to a share drive. The windows explorer security permissions for this application (.exe) file is set for Everyone with List Folder/Read Data...
3
by: kinnu | last post by:
Hi How to list the names of all the folders in a drive ???? OR I have taken a Dir (directory list box) and text box. And using the timer, the selection of the the directories in the...
5
by: Bay0519 | last post by:
Hi, I'm using access 2003. Could someone help me list just the folder name on c:\access\ and save it into a table. Right now when I use vbdirectory, it list all my sub folders as well as the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.