Results 1 to 8 of 8

Thread: Find file size in folders

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2011
    Posts
    22

    Find file size in folders

    Hi

    A bit of a dilemma here!!

    I have a network folder L:\ containing hundreds of folders each containing images. Some of the image sizes are too big to display in multiple format due to a recent camera setting error.
    I need to optimize these images, easily done if I can find them all but manually searching would take me days.
    Do any members have example code to enable me to search through the subfolders and return the path for image file sizes say greater than 300kb?

    Much Appreciated
    Regards

  2. #2
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Find file size in folders

    get file size like this...
    Code:
    Private Sub Command1_Click()
    Dim FileLength
    
    CommonDialog1.ShowOpen
    
    Open CommonDialog1.FileTitle For Input As #1    ' Open file.
    FileLength = LOF(1)   ' Get length of file.
    Close #1   ' Close file.
    
    Text1.Text = FileLength & "  Bytes"
    End Sub
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2011
    Posts
    22

    Re: Find file size in folders

    Hi

    Thanks for that, not quite what I needed really but gives me a good start.

    Regards

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Find file size in folders

    you can use DIR to loop through all files in a directory, filelen to get the size of each file
    if you want to loop through the subfolders, you could call the procedure recursively

    you can then make a list (array or collection) of all files above whatever size to process later, or process them within the procedure

    vb Code:
    1. folder = "C:\test\"
    2. fname = dir(folder & "*.jpg")      ' change to suit
    3. do while len(fname) > 0
    4.    if filelen(folder & fname) > testsize then ' do something
    5.    fname = dir
    6. loop
    this will only do one folder, not the subfolders
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Find file size in folders

    u can list all files including sub folders using this code... add a cmd button and listbox
    Code:
    'set reference to Microsoft scripting runtime.
    Option Explicit
    
    Dim fso As New FileSystemObject
    Dim fld As Folder
    
    Private Sub Command1_Click()
    List1.Clear
    ListFile "c:\anyfolder", List1
    End Sub
    
    Private Function ListFile(ByVal StrFld As String, LstBx As ListBox)
    Dim sFld As Folder, sFil As File
    
    Set fld = fso.GetFolder(StrFld)
    For Each sFil In fld.Files
        DoEvents
        LstBx.AddItem sFil.Path & "\" & sFil.Name
    Next
      
    If fld.SubFolders.Count > 0 Then
        For Each sFld In fld.SubFolders
            DoEvents
            ListFile = ListFile + ListFile(sFld.Path, LstBx)
        Next
    End If
      
    Set fld = Nothing
    Set sFil = Nothing
      
    End Function
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2011
    Posts
    22

    Re: Find file size in folders

    Thats fantastic...... cheers

  7. #7
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Find file size in folders

    Yet another alternative is DRO: the DirReaderObject which is much faster than using the FSO, can also be used in script like FSO.

    It returns a bunch of useful info in an ADO Recordset, which can include or omit the folders as rows:
    • FullName
    • Name
    • Extension
    • Size
    • SizeOnDisk
    • Created
    • LastAccessed
    • LastModified
    • Attributes
    • IsFolder

    It also returns some other useful values for each node:
    • IsLarge
    • Level

    The download at the web site includes full documentation.

    Sample program:
    Code:
    Option Explicit
    '
    'Requires references to:
    '
    '   DirReaderObject Library
    '   Microsoft ActiveX Data Objects 2.5 Library (or later)
    '   Microsoft Shell Controls and Automation
    '
    
    Private Sub Form_Load()
        Dim strCaptionCache As String
        Dim strFolder As String
        Dim rsFiles As ADODB.Recordset    
        Show
        strCaptionCache = Caption
        
        Caption = strCaptionCache & " Scanning directory..."
        Refresh
        With CreateObject("Shell.Application").NameSpace(ssfMYPICTURES).Self
            strFolder = .Path
        End With
        
        With New BVODir.DirReaderObject
            'Default limit on returned records is 1000.
            Set rsFiles = .GetFiles(strFolder, FolderNames:=False, MaxRecords:=3000)
        End With
        
        Caption = strCaptionCache & " Loading grid..."
        Refresh
        'Filter for large .JPG, .JPEG
        rsFiles.Filter = "(Size > 300000 AND Extension = 'JPG') OR " _
                       & "(Size > 300000 AND Extension = 'JPEG')"
        Set flexFiles.DataSource = rsFiles
        rsFiles.Close
        
        Caption = strCaptionCache & " Done"
    End Sub
    
    Private Sub Form_Resize()
        If WindowState <> vbMinimized Then
            flexFiles.Move 0, 0, ScaleWidth, ScaleHeight
        End If
    End Sub
    You can process the Recordset (Windows' own power collection) any way you like. Above we load it into a FlexGrid control.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2011
    Posts
    22

    Resolved Re: Find file size in folders

    Thanks. Plenty of information and helpful solutions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width