Solved

search for all files contained in a textfile vb6

Posted on 2004-09-26
14
263 Views
Last Modified: 2011-09-20
Hello Experts

I have some code below which will search through the c:\ drive
it searches for the file contained in variable ( sSrchString )

what id like to do is have a text file   filenames.txt which contains 5 or more files
to search for

filenames.txt  could have any amount of different files e.g

firstfile.ini
secondfill.dll
thirdfile.exe
fourthfile.txt
fifthfile.jpg

id like my code to go through the filenames.txt file to search the c:\ drive for all names
contained in filenames.txt

'--------------------------------------------------------------------------------------

Option Explicit

Dim fso As New FileSystemObject   ' Add microsoft scripting runtime reference
Dim fld As Folder

Private Sub Command1_Click()
   
   Dim nDirs As Long, nFiles As Long, lSize As Currency
   Dim sDir As String, sSrchString As String
   
   sDir = "c:\"
   sSrchString = "vb.ini"   '  the variable
   
   Label1.Caption = "Searching " & vbCrLf & UCase(sDir) & "..."
   
   lSize = FindFile(sDir, sSrchString, nDirs, nFiles)
   
   MsgBox Str(nFiles) & " files found in" & Str(nDirs) & " directories", vbInformation
   MsgBox "Total Size = " & lSize & " bytes"

End Sub

Private Function FindFile(ByVal sFol As String, sFile As String, nDirs As Long, nFiles As Long) As Currency
   Dim tFld As Folder, tFil As File, FileName As String
   
   On Error GoTo Catch
   Set fld = fso.GetFolder(sFol)
   FileName = Dir(fso.BuildPath(fld.Path, sFile), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)
   While Len(FileName) <> 0
      FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, FileName))
      nFiles = nFiles + 1
      List1.AddItem fso.BuildPath(fld.Path, FileName)  ' Load ListBox
      FileName = Dir()  ' Get next file
      DoEvents
   Wend
   Label1 = "Searching " & vbCrLf & fld.Path & "..."
   nDirs = nDirs + 1
   If fld.SubFolders.Count > 0 Then
      For Each tFld In fld.SubFolders
         DoEvents
         FindFile = FindFile + FindFile(tFld.Path, sFile, nDirs, nFiles)
      Next
   End If
   Exit Function
Catch:  FileName = ""
       Resume Next
End Function

'--------------------------------------------------------------------------------------



0
Comment
Question by:Jimmyx1000
  • 7
  • 7
14 Comments
 
LVL 2

Expert Comment

by:gimmeadrink
Comment Utility
This works....


Option Explicit

Dim fso As New FileSystemObject   ' Add microsoft scripting runtime reference
Dim fld As Folder

Private Sub Command1_Click()
   
    Dim nDirs As Long, nFiles As Long, lSize As Currency
    Dim sDir As String, sSrchString As String
   
    Dim strInputFileName As String
   
    sDir = "c:\"
 
   
    strInputFileName = "C:\input.txt"
   
    Open strInputFileName For Input As #1
    Do While Not EOF(1)
        Line Input #1, sSrchString
   
        Label1.Caption = "Searching " & vbCrLf & UCase(sDir) & "..."
       
        lSize = FindFile(sDir, sSrchString, nDirs, nFiles)
       
        MsgBox Str(nFiles) & " files found in" & Str(nDirs) & " directories", vbInformation
        MsgBox "Total Size = " & lSize & " bytes"
   
    Loop
    Close #1
   
   

End Sub

Private Function FindFile(ByVal sFol As String, sFile As String, nDirs As Long, nFiles As Long) As Currency
   Dim tFld As Folder, tFil As File, FileName As String
   
   On Error GoTo Catch
   Set fld = fso.GetFolder(sFol)
   FileName = Dir(fso.BuildPath(fld.Path, sFile), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)
   While Len(FileName) <> 0
      FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, FileName))
      nFiles = nFiles + 1
      List1.AddItem fso.BuildPath(fld.Path, FileName)  ' Load ListBox
      FileName = Dir()  ' Get next file
      DoEvents
   Wend
   Label1 = "Searching " & vbCrLf & fld.Path & "..."
   nDirs = nDirs + 1
   If fld.SubFolders.Count > 0 Then
      For Each tFld In fld.SubFolders
         DoEvents
         FindFile = FindFile + FindFile(tFld.Path, sFile, nDirs, nFiles)
      Next
   End If
   Exit Function
Catch:  FileName = ""
       Resume Next
End Function

'--------------------------------------------------------------------------------------

0
 
LVL 2

Expert Comment

by:gimmeadrink
Comment Utility
I decided to write a different set of code in case you want to be consistant by using the FileSystemObject and TextStream to read the input file instead of the generic vb readfile stuff.


----------------------------------------

Dim fso As New FileSystemObject   ' Add microsoft scripting runtime reference
Dim fld As Folder
Const fsoForReading = 1

Private Sub Command1_Click()
   
    Dim nDirs As Long, nFiles As Long, lSize As Currency
    Dim sDir As String, sSrchString As String
   
    Dim strInputFileName As String
    Dim objTextStream As TextStream

   
    sDir = "c:\"
     
   
    strInputFileName = "C:\input.txt"
   
    Set objTextStream = fso.OpenTextFile(strInputFileName, fsoForReading)
   
    Do While Not objTextStream.AtEndOfStream
        sSrchString = objTextStream.ReadLine
   
        Label1.Caption = "Searching " & vbCrLf & UCase(sDir) & "..."
       
        lSize = FindFile(sDir, sSrchString, nDirs, nFiles)
       
        MsgBox Str(nFiles) & " files found in" & Str(nDirs) & " directories", vbInformation
        MsgBox "Total Size = " & lSize & " bytes"
   
    Loop
    Close #1
   
   

End Sub

Private Function FindFile(ByVal sFol As String, sFile As String, nDirs As Long, nFiles As Long) As Currency
   Dim tFld As Folder, tFil As File, FileName As String
   
   On Error GoTo Catch
   Set fld = fso.GetFolder(sFol)
   FileName = Dir(fso.BuildPath(fld.Path, sFile), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)
   While Len(FileName) <> 0
      FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, FileName))
      nFiles = nFiles + 1
      List1.AddItem fso.BuildPath(fld.Path, FileName)  ' Load ListBox
      FileName = Dir()  ' Get next file
      DoEvents
   Wend
   Label1 = "Searching " & vbCrLf & fld.Path & "..."
   nDirs = nDirs + 1
   If fld.SubFolders.Count > 0 Then
      For Each tFld In fld.SubFolders
         DoEvents
         FindFile = FindFile + FindFile(tFld.Path, sFile, nDirs, nFiles)
      Next
   End If
   Exit Function
Catch:  FileName = ""
       Resume Next
End Function

'--------------------------------------------------------------------------------------




The key part is:

    strInputFileName = "C:\input.txt"
   
    ' Open the new text stream
    Set objTextStream = fso.OpenTextFile(strInputFileName, fsoForReading)
   
    ' whiles its not the end of the file:
    Do While Not objTextStream.AtEndOfStream
        ' get the filename to find
        sSrchString = objTextStream.ReadLine

        '''''''''''''''''''''''' your code here ''''''''''''''''''''''''

    ' finished with the file, loop and start again
    Loop


good luck
0
 
LVL 2

Expert Comment

by:gimmeadrink
Comment Utility
oh, one more think.

strInputFileName = "C:\input.txt"


should have "C:\filenames.txt" or whatever the full path of the file that contains the list of files to search for.
0
 

Author Comment

by:Jimmyx1000
Comment Utility
Would there be a chance that instead of having to search all the drive again
with each new file in the "C:\input.txt"

it would rather do a search like

eg.

code starts search at the begining of c:\ drive

the first file it finds is   (  firstfilefound.exe  )

firstfilefound.exe  >>>>>  check if this file is in 1st line of "C:\input.txt"
if yes save path and filename to a variable for later processing

then with the same firstfilefound.exe on drive c:
firstfilefound.exe  >>>>>  check if this file is in 2nd line of "C:\input.txt"
if yes save path and filename to a variable for later processing

then with the same firstfilefound.exe on drive c:
firstfilefound.exe  >>>>>  check if this file is in 3rd line of "C:\input.txt"
if yes save path and filename to a variable for later processing

once all filenames in "C:\input.txt" has been searched it would move to
secondfilefound.exe on c:\drive  >>>>>  check if this file is in 1st line of "C:\input.txt"

etc etc etc

Basically i would like to search the c: drive in the quickest possible manner

the "C:\input.txt" will contain a lot of files names for processing later on

i figure this is a fast way to do it.

thankyou experts

0
 
LVL 2

Expert Comment

by:gimmeadrink
Comment Utility
oh, ok....

do this instead...


Dim fso As New FileSystemObject   ' Add microsoft scripting runtime reference
Dim fld As Folder
Const fsoForReading = 1

Private Sub Command1_Click()
   
    Dim nDirs As Long, nFiles As Long, lSize As Currency
    Dim sDir As String, sSrchString As String
   
    Dim strInputFileName As String
    Dim objTextStream As TextStream

   
    sDir = "c:\"
     
   
    strInputFileName = "C:\input.txt"
   
    Set objTextStream = fso.OpenTextFile(strInputFileName, fsoForReading)
   
    Do While Not objTextStream.AtEndOfStream
        sSrchString = sSrchString & ";" & objTextStream.ReadLine
   
    Loop

        Label1.Caption = "Searching " & vbCrLf & UCase(sDir) & "..."
       
        lSize = FindFile(sDir, sSrchString, nDirs, nFiles)
       
        MsgBox Str(nFiles) & " files found in" & Str(nDirs) & " directories", vbInformation
        MsgBox "Total Size = " & lSize & " bytes"


End Sub

Private Function FindFile(ByVal sFol As String, sFile As String, nDirs As Long, nFiles As Long) As Currency
   Dim tFld As Folder, tFil As File, FileName As String
   
   On Error GoTo Catch
   Set fld = fso.GetFolder(sFol)
   FileName = Dir(fso.BuildPath(fld.Path, sFile), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)
   While Len(FileName) <> 0
      FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, FileName))
      nFiles = nFiles + 1
      List1.AddItem fso.BuildPath(fld.Path, FileName)  ' Load ListBox
      FileName = Dir()  ' Get next file
      DoEvents
   Wend
   Label1 = "Searching " & vbCrLf & fld.Path & "..."
   nDirs = nDirs + 1
   If fld.SubFolders.Count > 0 Then
      For Each tFld In fld.SubFolders
         DoEvents
         FindFile = FindFile + FindFile(tFld.Path, sFile, nDirs, nFiles)
      Next
   End If
   Exit Function
Catch:  FileName = ""
       Resume Next
End Function

'--------------------------------------------------------------------------------------

basically, seperating the filenames by ; is how you can find multiple files

Is this what you are after?

0
 

Author Comment

by:Jimmyx1000
Comment Utility
Seems to enumerate  the c:\  drive and also goes through the input.txt file
but does not seem to match anything

where can i implement:

if sSrchString =  files on hdd then
add to listbox
end if

thankyou expert



0
 

Author Comment

by:Jimmyx1000
Comment Utility
that last part should have read:

if sSrchString =  files on hdd then
add to path and filename to a listbox
end if
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Expert Comment

by:gimmeadrink
Comment Utility
ill have a look at it tonight sometime, im pretty sure its right, but i didnt actually run the code. I know that windows find does use ; to seperate searches... i might have stuffed up the code somewhere.

Ill get back to u in about 4 hours... sorry for the delay soooo much work to do.
0
 

Author Comment

by:Jimmyx1000
Comment Utility
Thankyou experts
0
 
LVL 2

Accepted Solution

by:
gimmeadrink earned 500 total points
Comment Utility
ok........

This searchs the filesystem only once, and finds all files specified in the input file:
------------------------------------

Dim fso As New FileSystemObject   ' Add microsoft scripting runtime reference
Dim fld As Folder
Const fsoForReading = 1

Private Sub Command1_Click()
   
    Dim nDirs As Long, nFiles As Long, lSize As Currency
    Dim sDir As String, sSrchString() As String
   
    Dim strInputFileName As String
    Dim objTextStream As TextStream

    Dim i As Integer
   
    sDir = "c:\"
     
   
    strInputFileName = "C:\input.txt"
   
    Set objTextStream = fso.OpenTextFile(strInputFileName, fsoForReading)
   

    ReDim Preserve sSrchString(i)
    Do While Not objTextStream.AtEndOfStream
        i = UBound(sSrchString) + 1
        ReDim Preserve sSrchString(i)
        sSrchString(i) = objTextStream.ReadLine
   
    Loop

    Label1.Caption = "Searching " & vbCrLf & UCase(sDir) & "..."
       
    lSize = FindFile(sDir, sSrchString, nDirs, nFiles)
       
    MsgBox Str(nFiles) & " files found in" & Str(nDirs) & " directories", vbInformation
    MsgBox "Total Size = " & lSize & " bytes"


End Sub

Private Function FindFile(ByVal sFol As String, sFile() As String, nDirs As Long, nFiles As Long) As Currency
   Dim tFld As Folder, tFil As File, FileName As String
   
   On Error GoTo Catch
   Set fld = fso.GetFolder(sFol)
   For i = 1 To UBound(sFile)
        FileName = Dir(fso.BuildPath(fld.Path, sFile(i)), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)
        While Len(FileName) <> 0
           FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, FileName))
           nFiles = nFiles + 1
           List1.AddItem fso.BuildPath(fld.Path, FileName)  ' Load ListBox
           FileName = Dir()  ' Get next file
           DoEvents
        Wend
   Next i
   Label1 = "Searching " & vbCrLf & fld.Path & "..."
   nDirs = nDirs + 1
   If fld.SubFolders.Count > 0 Then
      For Each tFld In fld.SubFolders
         DoEvents
         FindFile = FindFile + FindFile(tFld.Path, sFile, nDirs, nFiles)
      Next
   End If
   Exit Function
Catch:  FileName = ""
       Resume Next
End Function

------------------------------------


Im sure this is it....

Comments?
0
 

Author Comment

by:Jimmyx1000
Comment Utility
Congratulations my friend you done it

your code works as i want it too.

Thankyou very much for your time and effort.  

Thanks.

Id love to add an extra 500 points if you can do me a
pause button and a stop button.



0
 

Author Comment

by:Jimmyx1000
Comment Utility
Hmmmmm  seems very slow on a large "c:\input.txt"

any suggestions.
0
 
LVL 2

Expert Comment

by:gimmeadrink
Comment Utility
Ok, i have a couple ideas for both, ill have to get back to you late tonight tho (its GMT+10 here... gotta go to work now)

Thanks a lot for the points and the A
0
 

Author Comment

by:Jimmyx1000
Comment Utility
Really appreciate the help Mr. Expert

Good Work

Look forward to the results

thanks Expert


0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 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…

772 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

13 Experts available now in Live!

Get 1:1 Help Now