I think I have searched the entire web for a code that searches my computer for a file i for example put in a textbox.
Could anyone please give me such a code?
Printable View
I think I have searched the entire web for a code that searches my computer for a file i for example put in a textbox.
Could anyone please give me such a code?
Filename or files with the text contained in files?
'does file exist using fso
Dim sFile$
sFile = "Name And Path Of Your File"
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.fileexists(sFile$) = True Then
MsgBox "File Exists...Your code here!"
Else
MsgBox "Does Not Exist...Your code here!"
End If
Set fs = Nothing
Enjoy
Or if you want to search the hard drive for a file.
Hope it helps.;)Code:Option Explicit
Option Compare Text
Dim fso
Dim blFound As Boolean
Private Sub Command2_Click()
Dim myFolder
'create a file scripting object
Set fso = CreateObject("Scripting.FileSystemObject")
Me.Caption = "Working!"
'the folder to start looking in
myFolder = "c:\"
searchFileNames fso.getfolder(myFolder).Files, Text1.Text
searchFolderNames fso.getfolder(myFolder).subfolders, Text1.Text
blFound = False
Me.Caption = "DONE!"
End Sub
Private Sub searchFolderNames(myFolderList, strFile As String)
Dim myFolder
For Each myFolder In myFolderList
If blFound = True Then Exit Sub
'search the files for this folder
searchFileNames fso.getfolder(myFolder).Files, strFile
If myFolder.Name = strFile Then
MsgBox "Folder Found at : " & myFolder.Path
blFound = True
Exit Sub
End If
'recursivley search the folders
searchFolderNames fso.getfolder(myFolder).subfolders, strFile
Next
End Sub
Private Sub searchFileNames(myFolder, strFile As String)
Dim MyFile
For Each MyFile In myFolder
If MyFile.Name = strFile Then
MsgBox "File Found at : " & MyFile.Path
blFound = True
Exit Sub
End If
Next
End Sub
Or you can use the FindFirstFile API.
Code:Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long