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
160 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 16

Expert Comment

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

Expert Comment

by:Éric Moreau
ID: 7128828
Any progress?
0
 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

805 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