Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

search for all files contained in a textfile vb6

Posted on 2004-09-26
14
Medium Priority
?
283 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
ID: 12156495
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
ID: 12156524
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
ID: 12156549
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
Technology Partners: 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!

 

Author Comment

by:Jimmyx1000
ID: 12157022
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
ID: 12157583
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
ID: 12158002
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
ID: 12158010
that last part should have read:

if sSrchString =  files on hdd then
add to path and filename to a listbox
end if
0
 
LVL 2

Expert Comment

by:gimmeadrink
ID: 12158104
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
ID: 12158641
Thankyou experts
0
 
LVL 2

Accepted Solution

by:
gimmeadrink earned 2000 total points
ID: 12159076
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
ID: 12163847
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
ID: 12164030
Hmmmmm  seems very slow on a large "c:\input.txt"

any suggestions.
0
 
LVL 2

Expert Comment

by:gimmeadrink
ID: 12164582
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
ID: 12164673
Really appreciate the help Mr. Expert

Good Work

Look forward to the results

thanks Expert


0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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
Course of the Month20 days, 15 hours left to enroll

864 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