Find specific text and return that line

I have a folder with some .txt files and .png files.
I want to go through every .txt files inside that folder and find specific text.
If specific text is found, it will return the line of that specific text and the file name.

What method can I approach in this case?

Hi @Cari ,

You can use String or Regex Operations to check if if whatever text you are searching for is present in the file currently being iterated.

Kind Regards,
Ashwin A.K

HI @Cari

Please try this,

Main.xaml (10.7 KB)

In Foreach - Directory.GetFiles(“FolderPath”,"*.txt")

in If activity - Split(strText,Environment.NewLine).Where(function(d) d.ToString.ToLower.Trim.Contains(“specific text”)).Any

in First Assign - String.Empty

in Second Assign - Split(strText,Environment.NewLine).Where(function(d) d.ToString.ToLower.Trim.Contains(“specifictext”)).First.ToString

Thanks

HI @Cari

For looping through all the txt files you can try like this

  • Use For each file in Folder
    • Fill the fields with the appropriate fields and if your files are inside a folder like “YourPath\another folder” you need give the path upto your path and check the Include subfolders
    • In Filter By give your extension in your case you need to give “*.txt”
    • Assign Filename = CurrentFile.Name (it will give you Filemname.extension EG:" Sample.txt")
      - If you want only the file name without extension use this File_Name=Path.GetFileNameWithoutExtension(CurrentFile.ToString)
      image

And to get get specific value try this

  • Read Text File store in a variable
  • Generate Datatable activity and set the row seperators and column seperators in the options and save them in the variable (it will store as datatable)
  • From that data table you can filter the data using FilterDatatable activity or LINQ expression

Hope this Helps…

Regards
Sudharsan

Hi! It did not work. I even downloaded your file and change the file directory. But still did not find the specific text I want.

Hi! I actually extracting a report from SAP and save it as text, hence it is best if I not using datatable as there are millions of line.

@Cari can you share the text file and what text you are looking at?

Thanks

Hi, this is the txt file. I want to search “type=A”
This is just a short one with only 2 lines. Other txt files might have thousand of lines but its structure is same as this.

LOG.txt (1.1 KB)

Hi @Cari ,
use regex to get the data,
.*type=A.*
Refer below link

Regards,
Arivu

Hi @arivu96,
I tried but it doesnt work.
Did I make a mistake here?
Main.xaml (10.9 KB)
LOG.txt (1.1 KB)

You are using a wildcard within the Contains method. Contains is not supporting wildcards and mixing up Regexpattern with Part-Strings check

Also keep in mind:
Where(function(d) d.ToString.ToLower.Trim.Contains(".type=A.")).Any

will fail on the capital A, due the toLower will not match. here a type=a is recommended

in case of all file names should be filtered where type=a is present in the content we can do within a single assign (without for each, read text activities …)

Assign Activity
LHS: arrFileNames | String( ) a String array
RHS:

(From f In Directory.GetFiles("C:\Users\carissa.lee\OneDrive - Kuok (Singapore) Limited\Desktop\EY auditors\SAP ID Reports\","*.txt")
Let ftxt = File.ReadAllText(f)
Where ftxt.toLower.Contains("type=a")
Select x=f).toArray

And for sure we can adopt if needed for checking lines, using regex as well

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.