Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retrieve oldest file in directory 1

Status
Not open for further replies.

ktucci

Programmer
Apr 23, 2001
146
US
i am stuck on a what i think is a pretty simple problem...i would like to scan a directory at regular intervals and retieve the oldest file(file name) in that directory

thanks in advance, any help will be greatly appreciated

keith
 
You want to use the FileDateTime function to accomplish this as follows:

ThisFileDate = FileDateTime(sFILE)

The string sFile should be the full path and filename of the file checking. You can accomplish acquiring this several ways. For example, if you know the drive and folder you want to look in, it's the same all the time, you can use a FileListBox and set it's path to the one you desire, and also set the file pattern to the file types you desire, such as:

File1.Path = "c:\myfolder"
File1.Pattern = "*.dat"

Jim

'Solutions by Jim Null'
Web: Email: jim@jimnull.com
 
thanks, visual basic is not my specialty so i may not understand exactly what you are saying...could you give an example of your explanation...basically, i just want to call a function and pass the directory path ("c:\folder\")
and receive the file in that folder with the oldest date...speed may be an issue as the folder my have upwards of 8000 files inside of it

sorry for my lack of vb knowledge...thanks for your help so far
 
this function took about 2 seconds when passed a folder with 1200 files

Code:
Private Function OldestFile(ByVal sDir As String) As String
  Dim sFile As String
  Dim dFile As Date
  Dim sOldest As String
  Dim dOldest As Date
  
  If Right$(sDir, 1) <> &quot;\&quot; Then sDir = sDir & &quot;\&quot;
  
  dOldest = CDate(&quot;December 31, 9999&quot;)
  
  sFile = Dir(sDir & &quot;*&quot;)
  Do While sFile <> &quot;&quot;
    dFile = FileDateTime(sDir & sFile)
    If dFile < dOldest Then
      sOldest = sFile
      dOldest = dFile
    End If
    sFile = Dir
  Loop
  
  OldestFile = sOldest
End Function
 
thanks a million, it worked great
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top