[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


How to find a file by the modification date?

Posted on 2004-11-07
Medium Priority
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:Richard Kreidl
1 Comment
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 12519334
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

873 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