How to Extract amount from text file

I have text file with number of transection and amount i want to get only last line Credit amount
E.g
|D| 1000.11|
|D| 2000.88|
|D| 3000.11|
|C| 6000.11|

I want only 6000.11

Use Read CSV to put it into a datatable. Designate | as the delimiter. To get the second column of the last row:

yourDT.Rows(yourDT.Rows.Count-1)(1).ToString.Trim

If you want it as a numeric value (Double) then put a CDbl around it:

CDbl(yourDT.Rows(yourDT.Rows.Count-1)(1).ToString.Trim)

The reason for the -1 is that Rows is 0 index, so the first row is 0. That means if you have 4 rows, the last row is index 3. Same for the (1) index…the second column is index 1.

1 Like

@Anil_Potekar

if it looks like above then you can try this split

str.Split("|",StringSplitOptions.RemoveEmptyEntries).Last

cheers

2 Likes

Hi @Anil_Potekar

Use the Regex expressions to extract amount from the given text file. Find the below regex expression.

System.Text.RegularExpressions.Regex.Matches(yourstringinput.ToString,“((?<=|\s)\d+.\d+(?=|))”)

image

Hope it helps!!

1 Like

Input in text file or it is in datatable.

@Anil_Potekar

Textfile

Str is the output of read text file …and of stirng type

Cheers

@Anil_Potekar

  1. Use the “Read Text File” activity to read the content of the text file into a string variable, let’s call it textFileContent.
  2. Use the “Matches” activity to extract all matches of the credit amount from textFileContent. Configure it as follows:
  • Input: textFileContent
  • Pattern: \|C\| (\d+\.\d+)\|
  • Result: Create a new variable, let’s call it matches, of type System.Text.RegularExpressions.MatchCollection.
  1. Use the “Assign” activity to assign the last match’s value to a string variable, let’s call it creditAmountString. Set the value as follows:
  • matches(matches.Count - 1).Groups(1).Value
  1. Use the “Convert.ToDouble” activity to convert creditAmountString to a double variable. Let’s call the double variable creditAmount.
  2. Now, the creditAmount variable will contain the credit amount from the last line of the text file
1 Like

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