Search for files on harddisk


I need to make a program in VB that searches for the existens of a file(s) on the hard disk(s).
If for examples kill.exe and tlist.exe exists then msgbox "Warning".
The files to search for must came from a file, for example filenames.txt

Is that possible?

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.

This is an example of searching you need. Create a file named input.txt in c:\ which contain the filename you want to search. Each filename is separate by newline

Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Const MAX_PATH = 260

Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type
Function StripNulls(OriginalStr As String) As String
    If (InStr(OriginalStr, Chr(0)) > 0) Then
        OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
    End If
    StripNulls = OriginalStr
End Function

Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
    'KPD-Team 1999

    Dim FileName As String ' Walking filename variable...
    Dim DirName As String ' SubDirectory Name
    Dim dirNames() As String ' Buffer for directory name entries
    Dim nDir As Integer ' Number of directories in this path
    Dim i As Integer ' For-loop counter...
    Dim hSearch As Long ' Search Handle
    Dim Cont As Integer
    If Right(path, 1) <> "\" Then path = path & "\"
    ' Search for subdirectories.
    nDir = 0
    ReDim dirNames(nDir)
    Cont = True
    hSearch = FindFirstFile(path & "*", WFD)
    If hSearch <> INVALID_HANDLE_VALUE Then
        Do While Cont
        DirName = StripNulls(WFD.cFileName)
        ' Ignore the current and encompassing directories.
        If (DirName <> ".") And (DirName <> "..") Then
            ' Check for directory with bitwise comparison.
            If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
                dirNames(nDir) = DirName
                DirCount = DirCount + 1
                nDir = nDir + 1
                ReDim Preserve dirNames(nDir)
            End If
        End If
        Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
        Cont = FindClose(hSearch)
    End If
    ' Walk through this directory and sum file sizes.
    hSearch = FindFirstFile(path & SearchStr, WFD)
    Cont = True
    If hSearch <> INVALID_HANDLE_VALUE Then
        While Cont
            FileName = StripNulls(WFD.cFileName)
            If (FileName <> ".") And (FileName <> "..") Then
                FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
                FileCount = FileCount + 1
            End If
            Cont = FindNextFile(hSearch, WFD) ' Get next file
        Cont = FindClose(hSearch)
    End If
    ' If there are sub-directories...
    If nDir > 0 Then
        ' Recursively walk into them...
        For i = 0 To nDir - 1
            FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr, FileCount, DirCount)
        Next i
    End If
End Function

Sub Command1_Click()
    Dim SearchPath As String, FindStr As String
    Dim FileSize As Long
    Dim NumFiles As Integer, NumDirs As Integer
    SearchPath = "c:\"
    Open "c:\input.txt" For Input As #1
    Do While Not EOF(1)
        Line Input #1, FindStr
        Screen.MousePointer = vbHourglass
        FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
        If NumFiles > 0 Then
            MsgBox "WARNING: " & NumFiles & " file(s) with name " & FindStr & " found in " & NumDirs + 1 & " Directories", vbCritical
        End If
        Screen.MousePointer = vbDefault
    Close #1
End Sub

Thanks & Cheers

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
This will be helpful for u

Dim objFSO As Scripting.FileSystemObject
Dim  txtStream as TextStream
Dim strFileName as String
Dim File As Scripting.TextStream

Set File = objFSO.GetFile("C:\FileNames.txt")
Set txtStream = File.OpenAsTextStream(OpenFileForReading)

'Reading file names from text file
Do While Not txtStream.AtEndOfStream
      strFileName = txtStream.ReadLine
'Check whether text file is empty
      If Trim(strFileName) = "" Then
            MsgBox "No File Names found in file...!", vbInformation, "Search files"
            Exit Sub
            'Search for harmful files
            strFileName = Dir("C:\" + strFileName)
             'If found display the warning
             If Trim(strFileName) = "" Then
                    MsgBox "Harmful file  " + strFileName + "Found...!", vbInformation, "Warning"
             End If
      End If


Here I am reading file name from text file called FileName.txt and search those files in the specified path.If file found display the message.

hello harryv
This   simple api function can help you a lot

Private Declare Function SearchTreeForFile Lib "imagehlp" (ByVal RootPath As String, ByVal InputPathName As String, ByVal OutputPathBuffer As String) As Long
Private Const MAX_PATH = 260
Private Sub Form_Load()
    Dim tempStr As String, Ret As Long
    Dim MyValue  As Variant
    tempStr = String(MAX_PATH, 0)
    ' you can specify the path as c:\ or d:\
    'for eg.  C:\;abc.txt
    MyValue = InputBox("Dirve and file name separted by ;")
    MyValue = Split(MyValue, ";")
    Ret = SearchTreeForFile(MyValue(0), MyValue(1), tempStr)
    If Ret <> 0 Then
        MsgBox "Located file at " + Left$(tempStr, InStr(1, tempStr, Chr$(0)) - 1)
        MsgBox "File not found!"
    End If
End Sub
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.