Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8813641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:54:25+00:00 2026-06-14T03:54:25+00:00

I need to find the file that has the newest date in a given

  • 0

I need to find the file that has the newest date in a given directory. I’m less than a beginner in VB6 and that’s what I need to use for some old code. After killing Google all day below is what I have so far. But it doesnt find the right file. Can someone help please?
Thanks

Dim sFile1  As String
Dim sFile2 As String
Dim dFile1Date As Date
Dim dFile2Date As Date

sFile1 = Dir("c:\test\*.*", vbNormal)

Do
    sFile2 = Dir
    If sFile1 <> "" Then dFile1Date = FileDateTime("c:\test\" & sFile1)
    If sFile2 <> "" Then dFile2Date = FileDateTime("c:\test\" & sFile2)

    If dFile1Date > dFile2Date Then sFile2 = sFile1

Loop Until sFile2 = ""

FormVersionDate = sFile1
Call MsgBox(FormVersionDate, vbExclamation, App.Title)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:54:27+00:00Added an answer on June 14, 2026 at 3:54 am

    If you step through your code, you’ll see that sFile1 never gets reassigned no matter what the results of your logic, so calling it’s value at the end makes all of your do loop irrelevant. Also it looks like FormVersionDate is probably supposed to be a date, but you’re assign a string to it instead.

    I re-wrote your code with nested looping to avoid logic if it was irrelevant with excessive commenting:

    Dim sFile1  As String
    Dim sFile2 As String
    Dim dFile1Date As Date
    Dim dFile2Date As Date
    
    'Get first File
    sFile1 = Dir("c:\test\*.*", vbNormal)
    'Make sure file exists
    If sFile1 <> "" Then
        'Since file exists retrieve DateTime for file
        dFile1Date = FileDateTime("c:\test\" & sFile1)
        'begin loop for other files
        Do
            'Retrieve new file
            sFile2 = Dir
            'Make sure file exists
            If sFile2 <> "" Then
                'Retrieve DateTime for file
                dFile2Date = FileDateTime("c:\test\" & sFile2)
                'If old DateTime is older than new DateTime
                If dFile1Date < dFile2Date Then
                    'Overwrite first file variables with newer file info
                    sFile1 = sFile2
                    dFile1Date = dFile2Date
                End If
            End If
            'Loop until new file doesn't exist
        Loop Until sFile2 = ""
    End If
    'If you actually want the date, you should use dFile1Date instead of sFile1.
    FormVersionDate = sFile1
    Call MsgBox(FormVersionDate, vbExclamation, App.Title)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given some source file (or more generic - input stream), I need to find
I need to find a value in a file that has an id (say
I need to find a silence in a mp3 file. Simple as that. For
I need to find the name of the parent directory for a file in
I have a text file that has sets of text I need to extract
I need to find a library that allows me to easily get a directory
I have a file that has 1 million numbers. I need to know how
I need to use a Delphi component to encrypt a file that can then
I have a file that has 1,000,000 float values in it. I need to
I need to find out the listed for files or folders that has been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.