Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

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
0
1469
Asked:
1469
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now