Link to home
Create AccountLog in
Avatar of 1469
1469

asked on

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
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

What are you working in?  VBScript (.vbs), VB6, VBA or VB.Net?
Avatar of 1469
1469

ASKER

VBA
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
By the way, you'll also need a reference to Windows Script Host in your project.

Bill
Avatar of 1469

ASKER

I nedd a full search facility so I only know that it could be in c:
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
SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.