Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Search for files on harddisk

Posted on 2003-10-28
Medium Priority
Last Modified: 2010-05-03

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?

Question by:harryv
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

fantasy1001 earned 800 total points
ID: 9640229
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
    'E-Mail: KPDTeam@Allapi.net
    'URL: http://www.allapi.net/

    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

Expert Comment

ID: 9640277
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.


Expert Comment

ID: 9640554
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

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question