Solved

How can I get a folder's attribute like size and so on and quote it in VB

Posted on 2002-04-21
11
159 Views
Last Modified: 2010-05-02
Dear all,

I want to sort all subfolders by size under a folder. But I don't know how I can get it and quote it in VB.
any suggestion will be highly appreciated.

David777
0
Comment
Question by:David777
11 Comments
 
LVL 1

Expert Comment

by:gron
ID: 6957610
There may be a more elegant way, but one suggestion would be to loop through the files in the folder and subfolders and add up their length using the FILELEN command, as in :

Pathname = "D:/My Documents/ReadMe.txt"
a = FileLen(Pathname)
0
 
LVL 4

Accepted Solution

by:
RichW earned 50 total points
ID: 6957661
Set a reference to the Microsoft Scripting Runtime object.

Place a listbox on a form.

Paste the following into your form's module:

Replace MyFolder witht he folder you wish to target.

This will sort the subfolders according to size.
You can tweak the code to add the folder names, etc.

This will give you an idea of how to grab the attributes of the subfolders.

Good luck!
RichW

Private Sub Form_Load()
Dim fso As Scripting.FileSystemObject
Dim fldr
Dim subFldr
Dim ctr As Long
Dim ctr2 As Long
Dim lngSize As Long
Dim s

Set fso = New Scripting.FileSystemObject
Set fldr = fso.GetFolder("c:\MyFolder")
Set subFldr = fldr.SubFolders

For Each s In subFldr
    Me.List1.AddItem s.Size & "Whoohoo!"
Next
Call sort(Me.List1)

End Sub
Private Function sort(x As ListBox)

 Dim a As String
 Dim z As Integer
 
 For z = 0 To x.ListCount - 2
  If Val(x.List(z)) > Val(x.List(z + 1)) Then
   a = Val(x.List(z))
   x.List(z) = Val(x.List(z + 1))
   x.List(z + 1) = a
   z = 0
  End If
  DoEvents
 Next z
 
End Function

0
 
LVL 4

Expert Comment

by:RichW
ID: 6957663
Opps!  take out the ctr and ctr2 and lngSize and "Whoohoo!".  I used them for testing.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6958469
hearing...
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 7128828
Any progress?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:mksilk
ID: 7184066
RichW,

I am attempting to use your code but am receiving the following error:
"Compile error: User-defined type not defined"
This occurs on the line of code:

Dim fso as Scripting.FileSystemObject

Any ideas why??
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7185633
you have to set a reference to microsoft scrypting in your project.
Go to project/references... menu and locate that library. Do a check mark and push OK button.
Cheers
0
 
LVL 4

Expert Comment

by:RichW
ID: 7185648
I originally said to set a reference to the Microsoft Scripting Runtime object.  

You can't use the FileSystemObject without setting a reference to it first.

Thanks, Richie.
:)
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7185875
You are welcome.
By the way, i prefer to use references but using CreateObject, reference is not necessary.
Cheers
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7660610
Hi David777,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept RichW's comment(s) as an answer.

David777, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 

Expert Comment

by:SpideyMod
ID: 7755180
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

943 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

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now