Link to home
Start Free TrialLog in
Avatar of adam8
adam8

asked on

Get Attributes

Hi,
I need help with the Getattr function.
just say i have two listboxes.
One stores Directories and the other one stores Files.
If i have an array with a mixture of folders and files and i want to sort them........................
Take a look at this code.

List1.AddItem "Directories"
List2.AddItem "Files"
For X = 0 to UBound(Mixture)
    If GetAttr(Mixture(X)) = VbDirectory
 Then
    List1.AddItem Mixture(X)
Else
    List2.AddItem Mixture(X)
EndIf
'End Of Codee.

Well i might have made a mistake with the code except as you can see if it is a directory it adds puts the item into List1 if it is a file it adds puts the item into List2.
Well the problem is in the GetAttr Function.
Just say Mixture() Which contains file and folder names has a folder name called "C:\Windows\Fonts" which is a system directory or virtual directory or whatever you want to call it.
The code will add "C:\Windows\Fonts" in the Files Section (List2) because the GetAttr fnction doesn't recognize C:\WIndows\Fonts as a normal directory.

How could I alter the code so that it can also recognize hidden and system directories.

You don't have to use the GetAttr Function.
If it is easier use an API.
Doesn't matter what method you use.
Youcan see what i want so just create code that works...
Thanks in advance
Avatar of adam8
adam8

ASKER

Adjusted points from 51 to 100
how bout we just reverse it  =)

For X = 0 to UBound(Mixture)
    If GetAttr(Mixture(X)) <> VbDirectory
 Then
    List2.AddItem Mixture(X)
Else
    List1.AddItem Mixture(X)
EndIf
geez never mind..i'm not thinking

but along those lines, test to see if its a file (either read only, hidden, etc)  if its none of those it must be a folder yes?
Avatar of Ark
Hi

List1.AddItem "Directories"
List2.AddItem "Files"
For X = 0 to UBound(Mixture)
    If GetAttr(Mixture(X) and vbDirectory) = VbDirectory
 Then
    List1.AddItem Mixture(X)
Else
    List2.AddItem Mixture(X)
EndIf

I've already checked - works with All directories (system, hidden, normal, system + hidden etc)
Cheers
If GetAttr(Mixture(X) and vbDirectory) = VbDirectory
 

??????

Ark you lost me   =/
how bout this:

Dim strExt As String
For X = 0 to UBound(Mixture)
    strExt = Right(Mixture(X),4)
    If InStr(1, strExt, ".")
 Then
    List2.AddItem Mixture(X)
Else
    List1.AddItem Mixture(X)
EndIf
Next

Assuming all the files in your array have extensions
Sorry, wrong place of bracket. Correct -

If (GetAttr(Files(X)) And vbDirectory) = vbDirectory Then

ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
adam8:
OK, Ark knows what he is talking about. But I thought I might explain the use of GetAttr:

When you use GetAttr, it returns ALL the attributes added into one number. And to check the returned number against e.g. vbDirectory you need to use "bitwise comparison."

(Returned_value And vbDirectory) = vbDirectory if this is true, then the Returend_value contains vbDirectory

MSDN Library:
***
bitwise comparison

A bit-by-bit comparison between identically positioned bits in two numeric expressions.
***

I hope you understand this!
Azra,
I hope you understand my mistake. Everybody (even you) can make it.
Cheers
Oh I understand and I have made many similar ones myself.  Actually it wasnt your syntax error that threw me but your methods in general. I have never used the function before so I was unaware of how it was applied.  You taught me something new tonight. Thank you.    =)
Azra,
"And" is very usefull when you work with a lot of flags (for samples take a look at http://www.freevbcode.com/ShowCode.Asp?ID=631)
If you reseive some Sum of flags (vbDirectory Or (or=+) vbSystem or vbHidden etc) you can easy received if your flag matches summary flags using "And":
If (GetAttr(Mixture(X)) And vbDirectory) = vbDirectory Then
' one of flags is directory (don't care system, hidden etc)
If (GetAttr(Mixture(X)) And vbSystem) = vbSystem Then
Inthis case ALL system values (folders+files) matches etc.
Cheers
Avatar of adam8

ASKER

ark,
i posted this question before i tried the code on the directory question but it was too late because i had already posted this question.

Azrasound,
if Getattr(Mixture(X)) <> Vbdirectory........
C:\WIndows\fonts is not a normal directory...
in that case it would be listed in the files section.
Get what i am saying.

I will just give the first person who answered it correct the points and that person was Ark,
but he already got some many points from me and
Olli explained it,
even though i didn't understand what he was talking about but it is fair to give the points to Ark
adam8:
Haha! <smile> I was afraid you would not understand it... It's hard to explain and harder to understand! It took me quite a while to understand it... But the use of bits is quite common, and VERY useful!