如何按日期/时间过滤文件名?

时间:2014-02-17 14:41:58

标签: vb.net visual-studio-2010 datetime

我需要帮助过滤特定时间/日期(所有文件都是.jpeg格式)在我的程序报告生成(运动检测系统)中,用户可以在其中查看从特定时间点到另一个时间点的检测到的图像(例如1 :00pm - 2:00 pm)然后在列表框中显示文件。

enter image description here 示例截图文件名:pic_HHMMss_ddMMMyyyy

系统的工作原理如下。在网络摄像头检测到动作后,它会自动捕获图像并将其保存到C:\ Surveillance System \ Detected并生成文件名pic_HHMMss_ddMMMyyyy。因此,现在这是报告生成表单,其中授权人员可以通过过滤捕获照片的时间/日期来查看检测到的图像。

目前,我只能在没有任何过滤器的情况下显示目录中的所有文件。非常感谢任何见解或帮助。谢谢! :) 代码:

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 =检测到显示 button2_click =清除项目

2 个答案:

答案 0 :(得分:6)

有两种方法可以做到这一点。一个是作为文件名的一部分列出的时间:

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

<强>更新
我看到你改变了画面。此处提供的格式字符串仅支持原始图片中使用的原始文件名格式。新图显示了文件名格式的多种约定。如果您的文件确实有多种名称,则应考虑使用下面的“创建日期”选项,或者扩展此选项以使用TryParseExact() overload that accepts an array of possible formats


另一种方法是使用文件系统中的创建日期信息:

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

答案 1 :(得分:1)

尝试在文件上使用GetAttributes

请参阅http://msdn.microsoft.com/en-us/library/system.io.file.getattributes(v=vs.110).aspx

修改 这将添加您在DateTimePicker中设置的两次之间创建的文件。如果您希望在创建文件时过滤,则此方法有效,否则您需要对dra.FileName进行一些文本转换以使其与文件名匹配。您还需要添加一个事件来跟踪DateTimePicker中的更改,以便您的列表自动过滤和更新

'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

如果您想使用文件名,那么您需要提取日期并将文本转换为日期,以便您可以根据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