How to copy data from mutiple txt files into a excel file?

Dear Experts

I have a request a below

There are several txt files in a folder, I need to open them and copy data and paste data into an excel file one by one without overwriting each other
image

Below is the expected result after pasted into excel. We also need to delete the headers which hili with red frames

I try to use below solution but not sure how to let UiPath paste from last row in excel each time

May I have your guidance on this? Pls refer to below xaml file for your reference.

Test Sample.zip (144.5 KB)

Thank you for your help in advance :smiley:

Hi @yangyq10 ,

The problem you are facing, is that on each iteration, you are writing on the A2 Cell, therefore overwriting what the 1st iteration did.

You need to find the last row with data, and update the “2” with that value on each iteration:
In short, it would look like this:

1- Already in place, loop each file
2- Already in place, Inside the for each, read text file
3- Find last row with data (0 on the 1st iteration, 2 in the 2nd…)
4- Select Range (Instead of Always A2, A+last data with row index, +1)
5- Send Ctrl V

Another option you can use is convert the value from the Text file into a datatable, and instead of pasting from the clipboard, you can use the Append Range activity to let UiPath automatically detect the last row, and add your new data right below.

@ignasi.peiris I try lastrow last night, but I am not sure how to replace in selector with variable

You want to edit the String ¨Range¨on the ¨Select Range¨activity, no need to use selectors at all :slight_smile:

for your reference:

image

Find below the 2nd approach, using the excel activities (Did redesign it a bit to avoid opening excel 3 times):

Test Sample_IP.zip (159.4 KB)

Manage sheet:

Edit: The solution does not contemplate a rerun, therefore make sure to delete the rows in the “Manage” Sheet, before running. Also it is always adding the headers as your example. If you want the same result but without repeated headers, make sure to mark this property. Also cleaned up a bit and reuploaded the example.

BR,
Ignasi

1 Like

@ignasi.peiris

Thank you for your feedback. I add some activities to delete unnecessary headers :smile:


u

1 Like

@ignasi.peiris

Sorry, one more question. In below step, since they are within for each file in folder, does it put the 3 txtfile data in dt all at once or one by one?

It is looping each file, the same as you had, the only difference is that I changed the order of your excel app scope and the for each, to avoid opening and closing excel 3 times:

What you had:
1- For each file
2- 3x Open Excel
3- 3x Write data
4- 3x Close Excel

New design:
1- 1x Open excel
2- 3x Write data
3- 1x Close Excel

Just to avoid possible problems related to opening/closing excel :slight_smile:

Long story short - Not all at once, but looks like it :slight_smile:

BR,
Ignasi

1 Like

@ignasi.peiris

I am thinking if we write 3X data in dt first then can we put the excel activities outside for each file in folder?

That’s a possibility aswell yep, after generating the datatable, you can build-up a DT, and then only write it once, should be doable :slight_smile:

1 Like

@ignasi.peiris

I am so so sorry, I found out column G should be empty, and all relative column data should move 1 column in this excel

For example:
G data move to column H
H data move to column I
I data move to column J
etc…

Not sure what make this mismatch :smiling_face_with_tear:

@ignasi.peiris I am so sorry I want to use back this copy and paste solution, would you show me more detail? :smiling_face_with_tear:

Hm, I was just checking and looks like you have 2 tabs on the text file, therefore that’s why the shift in the columns:

The image displays a portion of a text document with headers and data separated by arrows, with some areas highlighted by red boxes. (Captioned by AI)

before going back to the copy-paste solution, do you think it might be a problem on the text file? Also the solution might be to find those 2x tabs in the text file string, replace them with a single tab, and would work, but has its risks.

will try to help you in a while!

Okay, bit of a workaround, and for sure we have linq gurus that will find a better approach, but its sorted.

The Generate Datatable activity was identifying the double tab as a single one, and that’s the reason for the missing column and the shift.

Solution: Regex replace the 2 tabs with the same 2 tabs + a keyword in the middle, that will generate the right number of columns, and then remove the keyword.

(another approach could be to remove the keyword with an excel macro)

I did also try to convert it to CSV, and change the Tabs to commas, but your Address field can contain commas, and would produce the same error without further development to take that out.
Test Sample_IP.zip (166.1 KB)

@ignasi.peiris

Thank you for your patience and great support!!

I have a call with my requester to learn about the txt files. He told me we can’t change anything in it base on our customers’ guidance and the best way to do so is copy and paste

So I keep debugging in last one hour and get the solution as below:

I add one sheet named “Trans” and paste the txt data in it in each loop then paste to the sheet"manage"

And it seems works though it looks quite troublesome :smiling_face_with_tear:

1 Like

Glad you found a solution :slight_smile: Good one!

1 Like

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