Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Finding a File on the C Drive

I think you and Tom miscommunicated.

You meant that myAccount.xls and Account.xls appear in the same folder (and the
list). You only want Account.xls (and avoid any filenames like myAccount.xls).

If that's true:

Option Explicit
Sub SearchForAccount()
Dim i As Long
Dim rCtr As Long
Dim sStr As String

sStr = "c:\"
rCtr = 0

With Application.FileSearch
.NewSearch
.LookIn = sStr
.SearchSubFolders = True
.Filename = "account.xls"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
ReDim FileList(1 To .FoundFiles.Count)
For i = 1 To .FoundFiles.Count
If LCase(.FoundFiles(i)) Like LCase("*\" & .Filename) Then
rCtr = rCtr + 1
ActiveSheet.Cells(rCtr, 1).Value = .FoundFiles(i)
End If
Next i
Else
MsgBox "There were no files found."
End If
End With

End Sub

The "like" stuff is looking for anything that ends with "\account.xls".
And that backslash is important.

John Baker wrote:

Tom:

Thanks, that great.

The only real questions I have now a

a. It appears to return multiple entries for a given folder and file, when there is only
ONE representation of the file in the folder in fact. Is there some way to return just ONE
entry for each file encountered?

b. It returns anything with the word "account" in the file name "MyAccount.xls" etc. Is
there some way I can restrict it to the exact file name?

Thanks a lot

John
"Tom Ogilvy" wrote:

Sub SearchForAccount()
Dim i As Long
Dim sStr As String
Dim MyVar As String
Dim FileList() As String
ReDim FileList(0 To 0)

sStr = "C:\"
With Application.FileSearch
.NewSearch
.LookIn = sStr
.SearchSubFolders = True
.FileName = "account.xls"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
If i = 1 Then
MyVar = .FoundFiles(i)
Else
ReDim FileList(1 To .FoundFiles.Count)
For i = 1 To .FoundFiles.Count
FileList(i) = .FoundFiles(i)
Next i
End If
Else
MsgBox "There were no files found."
End If
End With

If LBound(FileList) = 1 Then
For i = LBound(FileList) To UBound(FileList)
ActiveSheet.Cells(i, 1).Value = FileList(i)
Next
Else
Activesheets.Cells(1, 1).Value = MyVar
End If

End Sub


The results will contain the fully qualified filename (path included).

note that this can pick up myaccount.xls as well, so once the list is
returned, you need to examine each file name and make sure it is the file
you are interested in - but it looks like you will need to do that anyway to
account for multiple files.


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Links to mapped drive change to refer to local hard drive SueD Links and Linking in Excel 1 May 8th 08 11:42 AM
excel 2003 file converted to .xlsm file when save to network drive Chris Excel Discussion (Misc queries) 3 January 23rd 08 02:56 PM
WINDOWS CANNOT OPEN THIS FILE IN A:\ DRIVE Hils Excel Discussion (Misc queries) 3 January 10th 06 02:24 PM
File in C drive is always trying to access Drive A Andre Croteau Excel Discussion (Misc queries) 3 August 9th 05 01:13 PM
Deleting a file on C drive using VBA Todd Huttenstine[_2_] Excel Programming 7 November 19th 03 03:26 AM


All times are GMT +1. The time now is 10:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"