Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Directory Traversal Errata 1

Status
Not open for further replies.

bont

Programmer
Sep 7, 2000
200
0
0
US
I have spent a few days researching directory traversing in VB 6.0. I have found that most use this kind of approach:

1: File_Name = Dir$(File_Path, vbDirectory)
2: Do While File_Name <> &quot;&quot;
3: File_Name = Dir$
4: Loop


It is then used in a recursive manner.

What I have found frustrating is that all the examples I find, don't work?

I think line 1 of the code above should return a pointer to a list of directory names? But instead it returns all items in the directory? What is the point of specifying vbDirectory?

Once I realized that everything is brought back (dir, files), I found that someone used this function to determine the type of item:

GetAttr(File_Path & File_Name) = X

Where X is a code for a type of attribute for a file.
In all the examples I found, all used vbDirectory as X (16).
I understand what is done here, but in testing I found the following to be valid values for directories (X):

8240
8241
8208

Am I in the correct thinking, or is there a better way? Also, if I am correct about this being the best way to do things:

1: File_Name = Dir$(File_Path, vbDirectory)
2: Do While File_Name <> &quot;&quot;
3: If GetAttr(File_Path & File_Name) = 8240 Or GetAttr(File_Path & File_Name) = 8241 Or GetAttr(File_Path & File_Name) = 8208 Then
4: ' Is Directory call function again
5: Else
6: ' Is File
7: end if
8: File_Name = Dir$
4: Loop


where can I find a lising of return values for getattr() function, and does this list change from OS to OS?
 
bont,

Consider FileSystemObject (FSO) Class instead. It is very flexible and easy to use.

Vladk
 
For getattr() function values see:


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
OK, correct me if I am wrong. I am judging that this thread to microsoft is what would be needed to use this windows API technique. In the course of doing so the vb constants, i.e. vbDirectory should work properly. My way is much simpler, but I don't know if I can trust the attributes function (the thread doesn't list them, if someone knows of a list, please help). I would suspect these can change from OS to OS? Anyway, after looking at the filesystem obj, this might be the easiest to implement. The same recursive theory, but less need for detail. Much more reliable if the object crosses the OS boundaries without problems.
 
bont,

FSO is recommended in both VB6 and .NET to perform common file system tasks. That is, it should be good for all Windows platforms including XP. That object is essentially a wrapper around API calls.

Vladk
 
In the reference I gave, about 10 lines down in the first example code you will find:

Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
Public Const FILE_ATTRIBUTE_DIRECTORY = &H10
Public Const FILE_ATTRIBUTE_HIDDEN = &H2
Public Const FILE_ATTRIBUTE_NORMAL = &H80
Public Const FILE_ATTRIBUTE_READONLY = &H1
Public Const FILE_ATTRIBUTE_SYSTEM = &H4
Public Const FILE_ATTRIBUTE_TEMPORARY = &H100

These remain (AFAIK) consistent for all modern operating systems.

Alternatively you can use the Object Browser and do a search for vbFileAttribute, where the VB intrinsic constants are all listed.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
I still don't agree with this windows API methos. When I use vbDirectory, it returns everything, directories and files. So I guress my vbDirectory is not set correctly. To combat this I use getatt() function, and I find that a directory can be any of 3 different values, adn this function apparently returns codes for different types of directories, but appears to be consistent. FileSystemObject is more consistent, with less code. Thanks for the help all.
 
If you use:

If GetAttr(File_Path & File_Name) AND vbDirectory = vbDirectory

which only picks up the Dir attribute it will be accurate

Each value is a one-bit value in a Long integer, so the values you see with GetAttr on it's own is the Integer value. Doing a logical AND with the appropriate vb constant will produce your correct answer directly

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top