Get Files in Directory by Date or Size

  • Thread starter Thread starter Pieter Coucke
  • Start date Start date
P

Pieter Coucke

Hi,

I need to find all the files in a directory which are younger than a given
date (modification date or creation date).

Also I need to find a way to find all files in a directory which are having
a given file size.

Thanks in advance!!
 
Dear Pieter
1. Add a form to a project.
2. Add the following controls:
2.a. DriveListBox (named Drive1 that I have used in he
code) and DirListBox (dir1) for selecting the path. You
may remove these if you have a predefined pathname or
want to have it inputted thru TextBox.
2.b. TextBox (txtFileSize) to input the File Size (which
you want to search)
2.c. Calendar Control (Calendar1) for selecting the date
(in the acceptable date format).
2.d. TextBox (txtResult) with txtResult.multiline=True
and txtResult.ScrollBars =2 (Vertical) for viewing the
result. You can dispense with this also.
2.e. CommandButton (Command1) that will populate the
txtResult.
3. Now Paste the following code to the form:
'************Code follows:
Private Sub Command1_Click()
PopulateGrid Dir1.Path
End Sub

Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
Dir1.Refresh
End Sub

Public Sub PopulateGrid(sPath As String)
Dim sFileName As String
Dim dFileDate As Date
Dim lFileSize As Long
Dim flag As Boolean
txtResult.Text = ""
sFileName = Dir(sPath & "\*.*")
Do
dFileDate = FileSystem.FileDateTime(sPath & "\" &
sFileName)
lFileSize = FileSystem.FileLen(sPath & "\" &
sFileName)
flag = False
If dFileDate > Calendar1.Value Then
flag = True
End If
If lFileSize = Val(txtFileSize) Then
flag = True
End If
If flag = True Then
txtResult.SelStart = Len(txtResult.Text)
txtResult.SelText = sFileName & Space(10) &
lFileSize & Space(10) & dFileDate & vbCrLf
End If
sFileName = Dir()
Loop Until Len(sFileName) = 0
End Sub
'************Code ends
4. Run the Form.
This should work and satisfy whatever you want to do with
it.
IRFAN.

NEVER USE YOUR ACTUAL E-MAILID on this forum.
I used it once and am getting about 80 virus-infected
mails, supposedly from Microsoft.
 
thanks a lot! :-) I'll check it out :-)

Irfan Fazli said:
Dear Pieter
1. Add a form to a project.
2. Add the following controls:
2.a. DriveListBox (named Drive1 that I have used in he
code) and DirListBox (dir1) for selecting the path. You
may remove these if you have a predefined pathname or
want to have it inputted thru TextBox.
2.b. TextBox (txtFileSize) to input the File Size (which
you want to search)
2.c. Calendar Control (Calendar1) for selecting the date
(in the acceptable date format).
2.d. TextBox (txtResult) with txtResult.multiline=True
and txtResult.ScrollBars =2 (Vertical) for viewing the
result. You can dispense with this also.
2.e. CommandButton (Command1) that will populate the
txtResult.
3. Now Paste the following code to the form:
'************Code follows:
Private Sub Command1_Click()
PopulateGrid Dir1.Path
End Sub

Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
Dir1.Refresh
End Sub

Public Sub PopulateGrid(sPath As String)
Dim sFileName As String
Dim dFileDate As Date
Dim lFileSize As Long
Dim flag As Boolean
txtResult.Text = ""
sFileName = Dir(sPath & "\*.*")
Do
dFileDate = FileSystem.FileDateTime(sPath & "\" &
sFileName)
lFileSize = FileSystem.FileLen(sPath & "\" &
sFileName)
flag = False
If dFileDate > Calendar1.Value Then
flag = True
End If
If lFileSize = Val(txtFileSize) Then
flag = True
End If
If flag = True Then
txtResult.SelStart = Len(txtResult.Text)
txtResult.SelText = sFileName & Space(10) &
lFileSize & Space(10) & dFileDate & vbCrLf
End If
sFileName = Dir()
Loop Until Len(sFileName) = 0
End Sub
'************Code ends
4. Run the Form.
This should work and satisfy whatever you want to do with
it.
IRFAN.

NEVER USE YOUR ACTUAL E-MAILID on this forum.
I used it once and am getting about 80 virus-infected
mails, supposedly from Microsoft.
 
Hi!

Unfortunately none of the 2 answers where whta I needed :-(

What I need is a way to get the name of all the files in a directory, whcih
are odler than an given date. I don't need to visualize it, but just have to
put the names in an array.

Anybody any idea?
 
Hi Pieter,

Herfried did give you this information.
Have a look at the 'FileInfo' class and its 'Length' property and the
File' class and its 'Get*Time' methods.

With that it would not be that dificult I thought.
Why the File I don't see because all you ask is in the FileInfo class.

What is the problem.

Cor
 
What I need is a way to get the name of all the files in a directory, whcih
are odler than an given date. I don't need to visualize it, but just have to

Perhaps this will help you:

Private Sub Button1_Click(...) Handles Button1.Click
Dim aFiles() As String
Dim alFiltered As New ArrayList
Dim dCompareDate As Date = #10/31/2003#

aFiles = Directory.GetFiles("c:\dirnamehere")

For x As Integer = 0 To aFiles.Length - 1
If File.GetLastWriteTime(aFiles(x)) < dCompareDate Then
alFiltered.Add(aFiles(x))
End If
Next

For Each s As String In alFiltered
Debug.WriteLine(s)
Next
End Sub
 
Thansk guys!

It's a perfect solution for the problem I had, I was jsut looking if there
was maybe a 'nicer' way:
liek this it first takes all the files, and than picks only the files with
the correct date. What I was looking for was something that took only the
'good' files without firts taking them all. But I guess that won't be
possible.

Thanks a lot for the help!
 
* "DraguVaso said:
Unfortunately none of the 2 answers where whta I needed :-(

I am sure you will be able to solve the problem _yourself_ when
"wasting" some time to understand the hints I gave.
 
I evaluated your answer, and I understood it, but like I explained 5 minutes
ago it wasn't exactly what I needed. But still thanks for the help! I guess
the File and FileInfo-class are the best solutions for what I was looking
for! So thank you very much!

*kiss* hehe ;-)
 
Back
Top