Thread: File Search
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jim Cone Jim Cone is offline
external usenet poster
 
Posts: 3,290
Default File Search


If you want just the most recent file then...

Sub LatestFile()
'Jim Cone - San Francisco, USA - June 2005
'Displays the latest file name in the strPath folder.

Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim strPath As String
Dim strName As String
Dim varDate As Variant

' Specify the folder...
strPath = "C:\Program Files\Microsoft Office\Office\Library"
' Use Microsoft Scripting runtime.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)

' Check date on each file in folder.
For Each objFile In objFolder.Files
If objFile.DateLastModified varDate Then
varDate = objFile.DateLastModified
strName = objFile.Name
End If
Next 'objFile

' Display file name in message box.
MsgBox strName & " - is latest file - " & varDate

Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
End Sub
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"MarkS"

wrote in message
Hi,
I use this code to find the last file.
Set fso = New Scripting.FileSystemObject
For Each ThisFile In fso.GetFolder(sICAPPathName).Files
If ThisFile.Name Like "ICAP_FPC*.*" Then
Call GetICAPDate
If LatestFile Is Nothing And dThisFilesDate < dLastFileDate Then
Set LatestFile = ThisFile
dLastUpdated = dThisFilesDate
ElseIf dThisFilesDate dLastUpdated _
And dThisFilesDate < dLastFileDate Then
Set LatestFile = ThisFile
dLastUpdated = dThisFilesDate
End If
End If
Next

I have to use a sub routine to interpret the date as the date can be in
several different formats. I would like to read the files in update order as
this gets them in file name order, how do I change it to do that.
Thanks MarkS