Solved

Search for files on harddisk

Posted on 2003-10-28
3
334 Views
Last Modified: 2010-05-03
Hi,

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?

Jeffeny
0
Comment
Question by:harryv
3 Comments
 
LVL 5

Accepted Solution

by:
fantasy1001 earned 200 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
Const MAXDWORD = &HFFFF
Const INVALID_HANDLE_VALUE = -1
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100

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 WFD As WIN32_FIND_DATA
    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.
        Loop
        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
        Wend
        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
    Loop
    Close #1
End Sub

Thanks & Cheers
0
 
LVL 1

Expert Comment

by:lalithaw
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
      Else
            '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

Loop

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.

Lalitha
0
 
LVL 1

Expert Comment

by:mahesh_mp
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)
    Else
        MsgBox "File not found!"
    End If
End Sub
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now