Solved

File Size

Posted on 2000-03-09
5
140 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
  • 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

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

Title # Comments Views Activity
VBA loop through headers using value 3 57
Excel object stays open 19 71
VBA open file from excel cell 4 36
Adding to a VBA? 6 61
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

910 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

23 Experts available now in Live!

Get 1:1 Help Now