marksynnott
asked on
File System Object - find a file on the hard drive
I wonder if anyone can help
I want to search for a file on my harddrive and then open and chage its contents. but i dont know where the file is. Any ideas?>
I want to search for a file on my harddrive and then open and chage its contents. but i dont know where the file is. Any ideas?>
This works to locate any filename or file pattern:
'---
strFilename = "file to find.txt" ' patterns can also be used
Shell("command /c dir " & strFilename & " /s/b > C:\myfile.txt")
'---
(If using NT/2000/XP, replace "command" with "cmd".)
The result will be a file, myfile.txt, which contains all occurrences of the file you requested. Once created, you can open the file and you'll see a list of the file(s) you were searching for.
'---
strFilename = "file to find.txt" ' patterns can also be used
Shell("command /c dir " & strFilename & " /s/b > C:\myfile.txt")
'---
(If using NT/2000/XP, replace "command" with "cmd".)
The result will be a file, myfile.txt, which contains all occurrences of the file you requested. Once created, you can open the file and you'll see a list of the file(s) you were searching for.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanjs a mill
Declare
Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long
Private Declare Function SearchTreeForFile Lib "imagehlp" (ByVal RootPath As String, ByVal InputPathName As String, ByVal OutputPathBuffer As String) As Long
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Private Const MAX_PATH = 260
Private Const DRIVE_CDROM = 5
Private Const DRIVE_FIXED = 3
Call this
Private Function Find_File(FileName As String) As String
Dim tempStr As String
Dim Ret As Long
Dim AllDrives As String
Dim DriveArray
Dim lErrCode As Long
Dim Counter As Long
Dim szValueName As String
Dim sUNCName As String
AllDrives = String(255, Chr$(0))
Ret = GetLogicalDriveStrings(255
AllDrives = Replace(AllDrives, Chr$(0), "")
AllDrives = Replace(AllDrives, "\", "\,")
DriveArray = Split(AllDrives, ",")
For Counter = 0 To UBound(DriveArray) - 1
Ret = GetDriveType(DriveArray(Co
If GetDriveType(DriveArray(Co
'create a buffer string
tempStr = String(MAX_PATH, 0)
'returns 1 when successfull, 0 when failed
Ret = SearchTreeForFile(DriveArr
If Ret <> 0 Then
Find_File = Left$(tempStr, InStr(1, tempStr, Chr$(0)) - 1)
Exit Function
End If
End If
Next Counter
Find_File = "NOT FOUND"
End Function