Solved

File Size

Posted on 2000-03-09
5
145 Views
Last Modified: 2010-05-02
Can anyone please tell me how I should do this. I want to read the filename and the filesize. I want it to loop until it finds a filesize greater than 25K, it will then pop up a message box. I want this to loop to find all the filesize over 25K. Thanks
0
Comment
Question by:gabedc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 150 total points
ID: 2601222
This uses the FileSystemObject in Microsoft Scripting Runtime - add a reference to it in your project Add a textbox called txtPathTo, command button called cmdSearch an list box called lstFiles

Dim fsoFiles As New FileSystemObject
Dim fdcFolders As Folders
Dim fdrFolder As Folder
Dim filFile As File
Dim flaAttribute As FileAttribute

Private Sub cmdSearch_Click()
    Set fdrFolder = fsoFiles.GetFolder(txtPath.Text)
    DrillDown fdrFolder
End Sub

Private Sub DrillDown(fdrFrom As Folder)
    Dim fdcSubFolders As Folders
    Dim flsMatches As Files
    Dim fdrSubFolder As Folder
    Set fdcSubFolders = fdrFrom.SubFolders
    For Each fdrSubFolder In fdcSubFolders
        DrillDown fdrSubFolder
        Set flsMatches = fdrSubFolder.Files
        For Each filFile In flsMatches
            If filFile.Size > 25 * 1024 Then
                lstFiles.AddItem filFile.Path & ", File Size = " & CStr(filFile.Size)
            End If
        Next
    Next
End Sub

Private Sub txtPathTo_Validate(Index As Integer, Cancel As Boolean)
    If Index = 0 Then
        If Right(txtPathTo(0).Text, 1) <> "\" Then txtPathTo(0).Text = txtPathTo(0).Text & "\"
        On Error GoTo errNoFolder
        Set fdrFolder = fsoFiles.GetFolder(txtPathTo(0).Text)
    End If
    Exit Sub
   
errNoFolder:
    MsgBox "The specified folder does not exist:" & vbLf & txtPathTo(0).Text, vbExclamation
    txtPathTo(0).SetFocus
    Exit Sub
End Sub
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 2601228
Sorry missed that you wanted this as a message box, replace the lstFiles.Additem line with your message box if that is what you want. This code drillsdown all subdirectories from the one you enter
0
 
LVL 4

Expert Comment

by:wqw
ID: 2601289
here is another example without a reference to the scripting library. put a testbox, a listbox and a command button on your form.

Option Explicit

Dim MaxFileSize As Long
Dim SubFolders As Collection

Private Sub Command1_Click()
    Set SubFolders = New Collection
    MaxFileSize = Text1
    Process "C:"
    While SubFolders.Count > 0
        Process SubFolders(1)
        SubFolders.Remove 1
    Wend
End Sub

Sub Process(sFolder As String)
    Dim sFile As String
    On Error Resume Next
    Text1 = sFolder
    DoEvents
    sFile = Dir(sFolder & "\*", vbDirectory)
    While sFile <> ""
        sFile = sFolder & "\" & sFile
        If (GetAttr(sFile) And vbDirectory) <> 0 Then
            If Err.Number = 0 Then
                SubFolders.Add sFile
            End If
        Else
            If FileLen(sFile) > MaxFileSize Then
                List1.AddItem sFile & " - " & FileLen(sFile)
                DoEvents
            End If
        End If
        sFile = Dir
    Wend
End Sub

Enter in textbox the size you are searching for then press button and watch!

HTH,

</wqw>

p.s. it also drills down and adds the files to a listbox.
0
 

Author Comment

by:gabedc
ID: 2602158
Thank you.
0
 

Author Comment

by:gabedc
ID: 2616242
The program works. Thanks Tim and wqw.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses
Course of the Month7 days, 11 hours left to enroll

632 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