searching a file

Hello,

I know the name of a file, but it could be in one path or in other,

I want to find it using VB code,

Is it possible that If I give the name of the file build a function that will return me the path where it´s¿?

Regards
1469Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
What are you working in?  VBScript (.vbs), VB6, VBA or VB.Net?
0
1469Author Commented:
VBA
0
WillibobCommented:
You could use the FileSystemObject's FileExists method as follows:

public function SimpleFindFile(sFileName as String) As String
dim fso as New FileSystemObject
dim sPath1 as string
dim sPath2 as string

sPath1="C:\My Music\"
sPath2="C:\My Other Music\"

if fso.FileExists(sPath1 & sFileName) Then
SimpleFindFile=sPath1 & sFilename
exit function
end if

if fso.FileExists(sPath2 & sFileName) Then
SimpleFindFile=sPath2 & sFilename
exit function
end if

SimpleFindFile="Not found."

end function

This assumes that the number of possible paths is limited and it will also only return the first match so if the same file resides in both folders only the first one will be returned.

Is this what you meant or do you need a full search facility?

Bill
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

WillibobCommented:
By the way, you'll also need a reference to Windows Script Host in your project.

Bill
0
1469Author Commented:
I nedd a full search facility so I only know that it could be in c:
0
WillibobCommented:
If you only know the drive that it is on then your search could take a very long time as it would need to be recursive and it wouldn't run as quickly as the windows search function.

Take a look at this code.
http://www.freevbcode.com/ShowCode.asp?ID=6979

Bill
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
The API method posted by Willibob will be the fastest...

You can also do it via FSO if you like:

Example:

   Dim location As String
   location = FindFile("c:\", "someFile.txt")


Option Explicit

Public Function FindFile(ByVal folderName As String, ByVal fileName As String) As String
    Static fso As Object
    If fso Is Nothing Then
        Set fso = CreateObject("Scripting.FileSystemObject")
    End If
   
    Dim f, fc, file, f1, p, ret
    Set f = fso.GetFolder(folderName)
    p = f.path
    If Right(p, 1) <> "\" Then
        p = p & "\"
    End If

    If fso.FileExists(p & fileName) Then
        FindFile = p & fileName
        Exit Function
    End If

    Set fc = f.SubFolders
    For Each f1 In fc
        ret = FindFile(f1.path, fileName)
        If ret <> "" Then
            FindFile = ret
            Exit Function
        End If
    Next
End Function
0
nffvrxqgrcfqvvcCommented:
The fastest and most easiest method would be to use the enumdirtree function in the dbghelp.dll

Note: Windows already comes with this file and its located in the System32 folder, however the enumdirtree function is not exported in that version, you must download the updated version and you must not overwrite the version in the system32 directory. You must save/copy the dll to your applications directory where your .EXE is located if your interested let me know
0
nffvrxqgrcfqvvcCommented:
'Note: First call will be slow(be patient...), second call fastest as the first call took a snapshot of the directories

dbghelp 6.6.3.5
http://www.geocities.com/egl1044/dbghelp_6.6.3.5.zip

'--- Module ---
http://www.geocities.com/egl1044/SOURCE/enumdirtree.html

' You can call the function like this

'FindAllFiles "C:\windows", "*exe"
FindAllFiles "C:\", "regedit.exe"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.