Solved

Get Attributes

Posted on 2000-04-13
14
224 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:adam8
  • 5
  • 5
  • 2
  • +1
14 Comments
 
LVL 1

Author Comment

by:adam8
ID: 2714811
Adjusted points from 51 to 100
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2714823
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
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2714827
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?
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 27

Expert Comment

by:Ark
ID: 2714859
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
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2714877
If GetAttr(Mixture(X) and vbDirectory) = VbDirectory
 

??????

Ark you lost me   =/
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2714903
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
0
 
LVL 27

Expert Comment

by:Ark
ID: 2714912
Sorry, wrong place of bracket. Correct -

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

0
 
LVL 27

Accepted Solution

by:
Ark earned 100 total points
ID: 2714914
Sorry, wrong place of bracket. Correct -

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

0
 

Expert Comment

by:Olli083097
ID: 2714988
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!
0
 
LVL 27

Expert Comment

by:Ark
ID: 2715161
Azra,
I hope you understand my mistake. Everybody (even you) can make it.
Cheers
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2715171
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.    =)
0
 
LVL 27

Expert Comment

by:Ark
ID: 2715337
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
0
 
LVL 1

Author Comment

by:adam8
ID: 2717788
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
0
 

Expert Comment

by:Olli083097
ID: 2746672
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!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question