How to merge .txt files and save their data to datatable

Hello, I am wondering how to solve this kind of task in the best possible way. I have 10 .txt files with the same headers but different data below (see screenshot examples).

image
image

I would like to read those 10 txt files into one datatable, which should look like this (I only inserted two file examples) :
image

If someone could share ideas on how to solve this kind of task, I would really appreciate this. Thank you guys for your time!

You can try renaming it to csv, so that it reads it as a datatable itself using Read CSV Activity and then merge datatable acctivity can be used

1 Like

Hi @Milda_x,

  1. load all files into a loop and read file by file to get the output
  2. create two data-table containing the same columns
  3. parse the output to generate data-table activity and config the delimiter as “,”
  4. use merge activity and merge the generated table into the final resulted data table
1 Like

@Milda_x Try following these Steps:

  1. Use Build Datatable Activity and define the Columns, and Get the Output as resultDT

  2. Get the full Paths of the txt files present in a Folder by using this Expression:
    txtFiles = Directory.GetDirectory(“yurFolderPath”,"*.txt")

Where txtFiles is an array of String type Variable.

  1. Use For Each of txtFiles and change the Type Argument to String
    Inside For Each Do these
    Use Read Text File Activity and pass the variable item to it as the File name
    Get the Output of Read Text File, say tableData
    Use Generate Datatable Activity, with input as tableData, Click on CSV Parsing and
    Check First Row as Column Headers.
    Get the Output From Generate Datatable Activity as tempDT.
    Use Merge Datatable Activity, with Destination as resultDT and Source as tempDT.

  2. Outside the For Each Loop, Use Write Range Activity and use the resultDT as the Datatable.

3 Likes

Thank you, this works perfectly!

1 Like

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