How to find a file by the modification date?

Posted on 2004-11-07
Last Modified: 2010-04-24
Let's say I have a directory like:"C:\Files\Data\HTML" which contains numerous *.html files. There are HTML files that are added and updated in this directory. I want to find the *.html file that was last modified(added or created) in this directory. Any ideas??

Here is the code that I used in VB 5.0:

      X = Dir("C:\Files\Data\HTML\*.html"
     TEMP = 0
     Do until X = ""
     If FileDateTime("c:\files\data\html\" & X) > TEMP then
        TEMP = FileDateTime("c:\files\data\html\" & X)
        THEFILE = X
     end if
     X = DIR()
     MsgBox (THEFILE)

I tried to do this in my VB .Net app., but I get the following error: 'Operator is not valid for type 'Date' and type 'Integer' on this line of code:

If FileDateTime("c:\files\data\html\" & X) > TEMP then

Question by:rkckjk
    1 Comment
    LVL 85

    Accepted Solution

    Try this out:

    Imports System.IO

    Public Class Form1
        Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "

        Public Sub New()

            'This call is required by the Windows Form Designer.

            'Add any initialization after the InitializeComponent() call

        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                End If
            End If
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents FolderBrowserDialog1 As System.Windows.Forms.FolderBrowserDialog
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents Button1 As System.Windows.Forms.Button
        Friend WithEvents Button2 As System.Windows.Forms.Button
        Friend WithEvents Label2 As System.Windows.Forms.Label
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.FolderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog
            Me.Label1 = New System.Windows.Forms.Label
            Me.Button1 = New System.Windows.Forms.Button
            Me.Button2 = New System.Windows.Forms.Button
            Me.Label2 = New System.Windows.Forms.Label
            Me.Label1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
            Me.Label1.Location = New System.Drawing.Point(8, 8)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(528, 40)
            Me.Label1.TabIndex = 0
            Me.Label1.Text = "Label1"
            Me.Button1.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.Button1.Location = New System.Drawing.Point(544, 8)
            Me.Button1.Name = "Button1"
            Me.Button1.Size = New System.Drawing.Size(56, 40)
            Me.Button1.TabIndex = 1
            Me.Button1.Text = "Select Folder"
            Me.Button2.Location = New System.Drawing.Point(8, 64)
            Me.Button2.Name = "Button2"
            Me.Button2.Size = New System.Drawing.Size(56, 40)
            Me.Button2.TabIndex = 2
            Me.Button2.Text = "Find Newest"
            Me.Label2.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.Label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
            Me.Label2.Location = New System.Drawing.Point(72, 64)
            Me.Label2.Name = "Label2"
            Me.Label2.Size = New System.Drawing.Size(528, 40)
            Me.Label2.TabIndex = 3
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(608, 118)
            Me.Name = "Form1"
            Me.Text = "Form1"

        End Sub

    #End Region

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Label1.Text = Application.StartupPath
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
                Label1.Text = FolderBrowserDialog1.SelectedPath
            End If
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim folderPath As String = Label1.Text
            If Directory.Exists(folderPath) Then
                Dim curDate As DateTime
                Dim newestFile As String
                Dim newestDate As DateTime
                Dim curFile As String
                Dim files() As String

                files = Directory.GetFiles(folderPath)
                For Each curfile In files
                    curDate = System.IO.File.GetLastWriteTime(curfile)
                    If (newestFile Is Nothing) OrElse (curDate > newestDate) Then
                        newestFile = curFile
                        newestDate = curDate
                    End If
                Label2.Text = newestFile & vbCrLf & newestDate.ToString
                MessageBox.Show(folderPath, "Folder not found", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Sub

    End Class

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now