• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Search string in files and combine all to one variable

Nu2Java

Member
I have this code for searching a string in text files. It shows me each file name where the string was found. How do I update this to show all the filenames at once?

Code:
Sub PartUsedOn()

  Dim theString As String
  Dim path As String
  Dim StrFile As String
  Dim fso As New FileSystemObject  '''Add to: Tools/References - Microsoft Scripting Runtime
  Dim file As TextStream
  Dim line As String

  theString = UCase(InputBox("Part Number?"))
  
  path = "C:\Scripts\"
  StrFile = Dir(path & "*.dp")

  Do While StrFile <> ""

  Set file = fso.OpenTextFile(path & StrFile)
  
  Do While Not file.AtEndOfLine
  line = file.ReadLine
  If InStr(1, line, theString, vbTextCompare) > 0 Then
  msgStrFile = StrFile & " - " & line
  End If
  Loop
  
  file.Close
  Set file = Nothing
  Set fso = Nothing

  StrFile = Dir()
  Loop
  
End Sub
 
Hi SirJB7,
I'm not sure what you mean. The code shows me the name of each file name that contains the string I enter into the input box. I want it to collect all the names and display a list.
 
SirJB7, In this code I posted, I actually left out the message box to return the file names in my IF statement.
 
Hi, Nu2Java!
Before the Do sentence add this:
Code:
  msgStrFile=""
Within the Do...Loop cycle change this:
Code:
  msgStrFile = StrFile & " - " & line
to this:
Code:
  msgStrFile = msgStrFile & StrFile & " - " & line & vbcr
After the Loop sentence add the proper message box statement.
Regards!
 
Hi, Nu2Java!
Before the Do sentence add this:
Code:
  msgStrFile=""
Within the Do...Loop cycle change this:
Code:
  msgStrFile = StrFile & " - " & line
to this:
Code:
  msgStrFile = msgStrFile & StrFile & " - " & line & vbcr
After the Loop sentence add the proper message box statement.
Regards!
@SIRJB this works great... Thank You
 
Hi, Nu2Juava!
Glad you solved it. Thanks for your feedback and welcome back whenever needed or wanted.
Regards!
 
Hi, Nu2Juava!
Glad you solved it. Thanks for your feedback and welcome back whenever needed or wanted.
Regards!
The only issue I seem to be having is.. when a file contains duplicates of the string I am searching, it will return duplicate file names. Not sure how to resolve that. I only want to return each file name once if it contains the string.
 
Hi, Nu2Java!

Add an array and a pair of index declarations:
Code:
Dim msgStrFile(1000) as String
Dim I as Integer, J as Integer

Replace this:
Code:
msgStrFile=""
by this:
Code:
I=0

Replace this:
Code:
  msgStrFile = msgStrFile & StrFile & " - " & line & vbcr
by this:
Code:
  For J=1 to I
    If msgStrFile(J)=StrFile Then Exit For
  Next J
  If J>I Then
    I=I+1
    msgStrFile(I)=StrFile
  End If

Then guess how to build the message to be displayed using the array and the index I. I forgot how to do it... and I won't remember.

Regards!
 
Hi, Nu2Java!

Add an array and a pair of index declarations:
Code:
Dim msgStrFile(1000) as String
Dim I as Integer, J as Integer

Replace this:
Code:
msgStrFile=""
by this:
Code:
I=0

Replace this:
Code:
  msgStrFile = msgStrFile & StrFile & " - " & line & vbcr
by this:
Code:
  For J=1 to I
    If msgStrFile(J)=StrFile Then Exit For
  Next J
  If J>I Then
    I=I+1
    msgStrFile(I)=StrFile
  End If

Then guess how to build the message to be displayed using the array and the index I. I forgot how to do it... and I won't remember.

Regards!
Awesome! Thank you SIRJB7, I will give this a try.
 
Back
Top