Combine txt files keeping 1 header row?

Hey guys,

This seems like it should be a simple process but I’m having trouble

I need to combine all *. Txt files in a folder for processing. They all have the same header row so just keeping one instance of the header row, then combining all the data fields so there are no duplicated headers. What’s the best way to do this?

Eg.

File1.txt
File2.txt
File3.txt

Read all files as one datatable (or combine if easier then delete combine file)

Name | Address
Field1a | Field1b
Field2a | Field2b

Name | Address
Field3a | Field3b
Field4a | Field4b

Name | Address
Field5a | Field5b
Field6a | Field6b

Output:

Name | Address
Field1a | Field1b
Field2a | Field2b
Field3a | Field3b
Field4a | Field4b
Field5a | Field5b
Field6a | Field6b

Hi Jon

You can read file with File.ReadAllLines(text) to Array of String

then you can use this activity in a for each loop

image

it should work

I’m having trouble with this first part, reading to array of string. But will this avoid duplicate header rows?

Hi,

Hope the following sample helps you.

Sample20210902-1.zip (3.0 KB)

Regards,

Thanks all! I was able to solve using the first suggestion but I included a File Counter + Row Counter and inside each For Each loop, I increment the counters.

Then I basically have an If FileCounter>1 and RowCounter=1 (The header row of other files) then do nothing, so it skips the header row of each additional file, otherwise append line.

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