cancel
Showing results for 
Search instead for 
Did you mean: 

How to read multiple text file with different name in folder after loop?

JamunaT
Level 4
Hi,
I am having 2 text file with different name in folder , the both text file is having more than 100 number of rows , In that I need to check the last row contains status =-10  or status>=0 For example text file sample I have mentioned below.

apr1,loop status>=0-------------------
apr2,loop status=-10-----------------
apr5,loop status=-10----------------
apr7,loop status>=0-------------------
apr8,loop status=-10-----------

In this sample text, multiple rows contains status =-10  , how to check the last row contains status=-10 or status >=0.
Can anyone guide me please, and also For the script , I have tried GET FILES action and store output as collection this fine , Then after loop I have tried READ ALL TEXT FROM FILES but I am not sure, In the input file name field how to give the filename  because 2 text files having different name in that folder Kindly clarify this also.
Thanks in Advance


------------------------------
Jamuna T
RPA developer
Changepond Technology
chennai
------------------------------
6 REPLIES 6

HarpreetKaur
Level 7
Hi Jamuna,

You need to create two loops here.
1st loop is to get all the files in that folder, which you've already done so that's good.
Nested within this parent loop, you now need a child loop which access the first file in that folder and once you have all the text in a data item after using of Read all Text, you can use the Blue Prism object action Utility String Split Lines, which will get all the rows from your text in a collection.
314.jpg
Now you can get the no. of rows in this collection and easily access the contents of the last row based on that number.

Hope this helps.

------------------------------
Harpreet Kaur Product Consultant
------------------------------

PabloSarabia
Level 11
Hello!!

This is really easy with a simple piece of code, just follow this steps:

In the Object Properties, add:
External References: System.IO.dll
Namespace Imports: System.IO

Create a new page, and add a "code stage"
Define the Inputs / outputs:
inputs: variable "Path" as a Text
outputs: variable "dtResult" as a Collection

Need to paste your path on the "Path" input variable.

The code looks like that:
DataTable dt = new DataTable();
dt.Columns.Add("fileName", typeof(String));
dt.Columns.Add("lastLine", typeof(String));
DataRow row;
String[] arLines;
foreach(String file in Directory.GetFiles(Path))
{
row = dt.NewRow();
arLines = File.ReadAllLines(file);
row[0] = file;
row[1] = arLines[arLines.Length-1];
dt.Rows.Add(row);
}
dtResult = dt;

You don't need to use a second loop to get the last line. Is better to use "File.ReadAllLines" and then get the last from the array list.

Hope this helps you

Bye 🙂

------------------------------
Pablo Sarabia
Programmer
Altamira AM
------------------------------

Hi Harpreet Kaur ,
Thanks for your response , Could you please mention how to give the logic in decision  like last Row contains status =-10 or Status >=0. after used Split lines.

------------------------------
Jamuna T
RPA developer
Changepond Technology
chennai
------------------------------

Hi Pablo Sarabia,
Thanks for your response, I will try this too, could you please guide me how to get the last from the array list.

------------------------------
Jamuna T
RPA developer
Changepond Technology
chennai
------------------------------

Hello Jamuna!

The "array string" have the ".Length" property. This tells you how many lines do you have.

Use this:

String[] arLines = File.ReadAllLines(file); //Load the array
String lastLine = arLines[arLines.Length-1]; //Get the last line

Need to use "arLines.Length-1" cause the index start in 0 not, in 1.


Hope this helps you

Bye 🙂


------------------------------
Pablo Sarabia
Programmer
Altamira AM
------------------------------

Thanks Pablo Sarabia , I will try as per this.

------------------------------
Jamuna T
RPA developer
Changepond Technology
chennai
------------------------------