View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson[_5_] Dave Peterson[_5_] is offline
external usenet poster
 
Posts: 1,758
Default Need Help with a File Search

This technique was first posted by Dana DeLouis:

With Application.FileSearch
'// Wake Excel up for msoSortByLastModified to work !
.NewSearch
.LookIn = "C:\"
.Filename = "*.jnk"
.Execute SortBy:=msoSortBySize
End With

With Application.FileSearch
.NewSearch
'....your existing code here



Darrin Henry wrote:

Sorry, to dig up this kind of old thread. But I have a problem with the
coding. It seems to not want to sort them by LastModified, as I need. It
always seems to go via file name, to reiterate my code is as follows:

Private Sub FindTotals_Click()
Dim i As Long
Dim FS As FileSearch
Dim myPath As String
Dim myFolderName As String
Dim myFileName As String

myPath = "C:\Hard Data"

Set FS = Application.FileSearch

With FS
.NewSearch
.LookIn = myPath
.Filename = "*.xls"
If .Execute(SortBy:=msoSortByLastModified, _
SortOrder:=msoSortOrderAscending) 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
For i = 1 To .FoundFiles.Count
If FolderAndName(.FoundFiles(i), myFolderName, myFileName) _
= False Then
MsgBox "something's ain't right!"
Else
ActiveSheet.Cells(i, 1).Formula _
= "='" & myFolderName & _
"[" & myFileName & "]GEMFEDCCOrderForm'!$K$38"
End If
Next i
Else
MsgBox "There were no files found."
End If
End With
End Sub

Function FolderAndName(WholeFileName As String, _
JustFolder As String, JustName As String) As Boolean
Dim iCtr As Long
FolderAndName = False
For iCtr = Len(WholeFileName) To 1 Step -1
If Mid(WholeFileName, iCtr, 1) = "\" Then
JustFolder = Left(WholeFileName, iCtr)
JustName = Mid(WholeFileName, iCtr + 1)
FolderAndName = True
Exit For
End If
Next iCtr
End Function

Any idea why it's not sorting via last modified? Thanks.

*** Sent via Developersdex http://www.developersdex.com ***


--

Dave Peterson