ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   File Search (https://www.excelbanter.com/excel-programming/307509-file-search.html)

MD

File Search
 
I'd like to know how to search for a known file (MyFile.pdf) that is located
somewhere in either on the C, D or E drive and get it's full pathname as a
string.
The end result would be:
MyString = "c:\MyDocuments\MyProgram\MySearch\MyFile.pdf" what is in quotes
would be the search results

Bonus Question
If more than one file is found, the one with the latest one would be date
would be returned.
************
or
************
If someone knows how to open a pdf file (MyFile.pdf) with Adobe Reader. Now
the folder that contains the exe reader is never in the same folder pending
on it's version and may be in other drive pending on the installation from
the user. So when I use the SHELL(path, filename) it doesn't always work.

Regards

Michel



Tom Ogilvy

File Search
 
look in Excel VBA help at the filesearch object
loop through the results list and gather the filedatetime

Dim dtData as Date, dtMaxDate as Date
Dim i as long
dim sStr as String

With Application.FileSearch
.NewSearch
.LookIn = "C:\"
.SearchSubFolders = True
.FileName = "MyFile.pdf"
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
dtDate = filedatetime(.foundfiles(i))
if dtDate dtMaxDate then
sStr = .foundfiles(i)
dtMaxDate = dtDate
End If
Next
shell "start " & sStr
end if
End With


----------------------

shell "start c:\MyDocuments\MyProgram\MySearch\MyFile.pdf"

--
Regards,
Tom Ogilvy


"MD" wrote in message
.. .
I'd like to know how to search for a known file (MyFile.pdf) that is

located
somewhere in either on the C, D or E drive and get it's full pathname as a
string.
The end result would be:
MyString = "c:\MyDocuments\MyProgram\MySearch\MyFile.pdf" what is in

quotes
would be the search results

Bonus Question
If more than one file is found, the one with the latest one would be date
would be returned.
************
or
************
If someone knows how to open a pdf file (MyFile.pdf) with Adobe Reader.

Now
the folder that contains the exe reader is never in the same folder

pending
on it's version and may be in other drive pending on the installation from
the user. So when I use the SHELL(path, filename) it doesn't always work.

Regards

Michel





MD

File Search
 
works!!!!
Many Thx Tom

"Tom Ogilvy" a écrit dans le message de
...
look in Excel VBA help at the filesearch object
loop through the results list and gather the filedatetime

Dim dtData as Date, dtMaxDate as Date
Dim i as long
dim sStr as String

With Application.FileSearch
.NewSearch
.LookIn = "C:\"
.SearchSubFolders = True
.FileName = "MyFile.pdf"
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
dtDate = filedatetime(.foundfiles(i))
if dtDate dtMaxDate then
sStr = .foundfiles(i)
dtMaxDate = dtDate
End If
Next
shell "start " & sStr
end if
End With


----------------------

shell "start c:\MyDocuments\MyProgram\MySearch\MyFile.pdf"

--
Regards,
Tom Ogilvy


"MD" wrote in message
.. .
I'd like to know how to search for a known file (MyFile.pdf) that is

located
somewhere in either on the C, D or E drive and get it's full pathname as

a
string.
The end result would be:
MyString = "c:\MyDocuments\MyProgram\MySearch\MyFile.pdf" what is in

quotes
would be the search results

Bonus Question
If more than one file is found, the one with the latest one would be

date
would be returned.
************
or
************
If someone knows how to open a pdf file (MyFile.pdf) with Adobe Reader.

Now
the folder that contains the exe reader is never in the same folder

pending
on it's version and may be in other drive pending on the installation

from
the user. So when I use the SHELL(path, filename) it doesn't always

work.

Regards

Michel








All times are GMT +1. The time now is 04:39 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com