|
-
Jun 16th, 2011, 04:16 AM
#1
Thread Starter
Junior Member
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
-
Jun 16th, 2011, 04:58 AM
#2
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
-
Jun 16th, 2011, 05:09 AM
#3
Thread Starter
Junior Member
Re: Find file size in folders
Hi
Thanks for that, not quite what I needed really but gives me a good start.
Regards
-
Jun 16th, 2011, 06:28 AM
#4
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
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
-
Jun 16th, 2011, 06:39 AM
#5
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
-
Jun 16th, 2011, 07:28 AM
#6
Thread Starter
Junior Member
Re: Find file size in folders
Thats fantastic...... cheers
-
Jun 16th, 2011, 08:08 AM
#7
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.
-
Jun 16th, 2011, 08:52 AM
#8
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|