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
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
Re: Find file size in folders
Hi
Thanks for that, not quite what I needed really but gives me a good start.
Regards
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:
folder = "C:\test\"
fname = dir(folder & "*.jpg") ' change to suit
do while len(fname) > 0
if filelen(folder & fname) > testsize then ' do something
fname = dir
loop
this will only do one folder, not the subfolders
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
Re: Find file size in folders
Thats fantastic...... cheers
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:
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.
Re: Find file size in folders
Thanks. Plenty of information and helpful solutions.