0

I have the below code which returns all file names of .txt files in a folder directory. I would like to add to this code to open the txt file and search for a user provided string and return true or false if the string is found in the file.

Any help would be much appreciated.

Sub VBAF1_List_All_TXT_Files_Using_Dir()

'Variable Declaration
Dim sFilePath As String
Dim sFileName As String
Dim sUserString As String

'Specify File Path
sFilePath = "C:\Users\d85307\Documents\Automation Anywhere Files\Automation Anywhere\My Docs\UK\Vehicle Delivery\ATDC\Log"
' sUserString =

'Check for back slash
If Right(sFilePath, 1) <> "\" Then
    sFilePath = sFilePath & "\"
End If
    
sFileName = Dir(sFilePath & "*.txt")

Do While Len(sFileName) > 0
    If Right(sFileName, 3) = "txt" Then
        'Display file name in immediate window
        Debug.Print sFileName
        
    End If
    'Set the fileName to the next available file
    sFileName = Dir
Loop

End Sub
Sean Bailey
  • 375
  • 3
  • 15
  • 33
  • Hi. This will help with the opening of `sFileName` https://www.excel-easy.com/vba/examples/read-data-from-text-file.html – Nathan_Sav Sep 06 '21 at 10:41
  • Does this answer your question? [Read/Parse text file line by line in VBA](https://stackoverflow.com/questions/11528694/read-parse-text-file-line-by-line-in-vba) – Tomalak Sep 06 '21 at 11:04

0 Answers0