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
157 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…

705 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

20 Experts available now in Live!

Get 1:1 Help Now