Cutting down returned results???


Posted by RoB on November 11, 2001 2:38 AM

I have a macro that uses the data in cell A1 to do a seach in the directory "e:\Macros", then it lists the files returned starting in cell A2. I have 2 questions:

1. currently, the returned results show as a full path ie: "e:\Macros\test.xls". I would like to have it just display the file (without extension) which would be "test" in this case. How would i go about "cutting" the result down to what i want? Please remember there are also sometimes more than one directory as the search looks in subdirectories. so a possible return could be "e:\Macros\subdir\test2.xls"

2. Not only would i like only to display ONLY the file name, but i would like to turn it into a link to the file. Can someone help me with this??

Heres the code if you need it:

Sub FileSearch()
Dim Criteria As String


Criteria = Range("A1")
With Application.FileSearch
.LookIn = "e:\Macros"
.SearchSubFolders = True
.Filename = Criteria & "*"
.FileType = msoFileTypeExcelWorkbooks
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
For i = 1 To .FoundFiles.Count
Cells(i + 1, 1) = .FoundFiles(i)
'MsgBox .FoundFiles(i)
Next i
Else
MsgBox "There were no files found."
End If
End With


End Sub

Posted by Dank on November 11, 2001 8:57 AM

Try this. It uses your code but with a function that extracts just the filename without the extenstion. I've then added another line to add the hyperlink.

Hope it helps,
Daniel.

Function FilenameOnly(strPath As String) As String
Dim lngCharCounter As Long
'First establish the filename and extenstion part
For lngCharCounter = Len(strPath) To 1 Step -1
If Mid(strPath, lngCharCounter, 1) = "\" Then
FilenameOnly = Right(strPath, Len(strPath) - lngCharCounter)
'Remove the extension
FilenameOnly = Left(FilenameOnly, InStr(1, FilenameOnly, ".") - 1)
Exit For
End If
Next lngCharCounter
End Function

Sub FileSearch()
Dim Criteria As String


Criteria = Range("A1")
With Application.FileSearch
.LookIn = "c:\temp"
.SearchSubFolders = True
.Filename = Criteria & "*"
.FileType = msoFileTypeExcelWorkbooks
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
For i = 1 To .FoundFiles.Count
Cells(i + 1, 1) = FilenameOnly(.FoundFiles(i))
ActiveSheet.Hyperlinks.Add Cells(i + 1, 1), .FoundFiles(i)
'MsgBox .FoundFiles(i)
Next i
Else
MsgBox "There were no files found."
End If
End With

End Sub


Posted by Juan Pablo on November 11, 2001 9:58 AM

To remove the extension, instead of finding a "." as Dank suggested (Which would create a conflict in the case of i.e. C:\Whatever\My.test.xls), i'd suggest testing the last 4 characters, and if they're equal to ".xls", then remove it. Something like:

If Right(.FoundFiles(i),4)=".xls" then
Cells(i+1,1)=Left(.FoundFiles(i),Len(.FoundFiles(i)-4)
else
Cells(i+1,1)=.FoundFiles(i)
End If

Juan Pablo I have a macro that uses the data in cell A1 to do a seach in the directory "e:\Macros", then it lists the files returned starting in cell A2. I have 2 questions:



Posted by RoB on November 11, 2001 3:43 PM

Thanks! works great!