I need help in filtering specific time/dates(all files are in .jpeg format) in my program's report generation (motion detection system) in which the user can view detected images from a specific point of time to another (e.g. 1:00pm - 2:00pm) then display the files in the listbox.
sample screenshot filename: pic_HHMMss_ddMMMyyyy
The system works like this. After a motion is detected by the webcam, it automatically captures an image and save it to C:\Surveillance System\Detected and generate the filename pic_HHMMss_ddMMMyyyy. So this is now the report generation form wherein the authorized person can view detected images by filtering the time/date of which when the photo is captured.
For now, I can only display all the files in the directory without any filters. Any insights or help is greatly appreciated. Thanks! :) codes:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' make a reference to a directory
Dim di As New IO.DirectoryInfo("c:\Surveillance System\Detected")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
'list the names of all files in the specified directory
For Each dra In diar1
ListBox1.Items.Add(dra)
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
PictureBox1.Image = Image.FromFile("C:\Surveillance System\Detected\" & ListBox1.Text)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DateTimePicker2.Format = DateTimePickerFormat.Time
DateTimePicker2.ShowUpDown = True
DateTimePicker1.Format = DateTimePickerFormat.Time
DateTimePicker1.ShowUpDown = True
End Sub
button1_click = show detected button2_click = clear items
I'd use YYYY-MM-DD HHmmss for filenames, unless there is a particular need for timezones or a possible need to parse them into ISO dates; in those cases an ISO date would probably be preferrable.
There are two ways to do this. One is by the time listed as part of the file name:
Function GetFiles(ByVal FilterStart As DateTime, ByVal FilterEnd As DateTime) As IEnumerable(Of String)
Dim culture As CultureInfo = CultureInfo.InvariantCulture
Dim FormatString As String = "HHmmss_ddMMMyyyy"
Return Directory.EnumerateFiles("c:\Surveillance System\Detected") _
.Where(Function(f)
Dim Filedate As DateTime = DateTime.ParseExact(f.Replace("pic_", "").Replace(".jpeg", ""), FormatString, culture)
Return Filedate >= FilterStart AndAlso Filedate <= FilterEnd
End Function)
End Function
Update:
I see you changed the picture. The format string provided here only supports original file name format used in the original picture. The new picture shows multiple conventions for the file name format. If you are really going to have multiple kinds of names for your files, you should consider using the Created Date option below, or extend this option to use the TryParseExact()
overload that accepts an array of possible formats.
The other is to use the Created Date information from the file system:
Function GetFiles(ByVal FilterStart As DateTime, ByVal FilterEnd As DateTime) As IEnumerable(Of String)
Dim di As New DirectoryInfo("c:\Surveillance System\Detected")
Return di.EnumerateFileSystemInfos() _
.Where(Function(f) f.CreationTime >= FilterStart AndAlso f.CreationTime <= FilterEnd) _
.Select(Function(f) f.Name)
End Function
Try using GetAttributes
on your files
see http://msdn.microsoft.com/en-us/library/system.io.file.getattributes(v=vs.110).aspx
EDIT
This will add files created between the two times you set in your DateTimePicker
. This will work if you want filter on when the file was created, otherwise you need to do some text transformations to dra.FileName
to match it to your filenames. You would also want to add an event to track changes in the DateTimePicker
s so your list will automatically filter and update
'list the names of all files in the specified directory
For Each dra In diar1
If dra.CreationTime > DateTimePicker1.Value And dra.CreationTime < DateTimePicker2.Value Then
ListBox1.Items.Add(dra)
End If
Next
If you want use the filename then you need to the extract date and convert the text to date so you can file based on the DateTimePicker
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DateTimePicker_ValueChanged(sender,Nothing)
End Sub
Private Sub DateTimePicker_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged, DateTimePicker2.ValueChanged
Dim di As New IO.DirectoryInfo("c:\Surveillance System\Detected")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
ListBox1.Items.Clear()
'list the names of all files in the specified directory
For Each dra In diar1
'Get file name and then convert to time
Dim splitname As String() = Replace(dra.Name.ToString, ".jpeg", "").Split("_")
Dim filetime As DateTime = Date.ParseExact(splitname(2) & splitname(1), "ddMMMyyyyHHmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo)
If filetime > DateTimePicker1.Value And filetime < DateTimePicker2.Value Then
ListBox1.Items.Add(dra)
End If
Next
end Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With