How to write multiple datatables to different csv files

I have a for each activity that executes 9 different queries and writes to a csv. Currently, each query writes to a datatable and the datatable writes to the same csv file by overwriting the data. For each query, I need the datatable to be written to a separate csv file.

For example:
sql1 >> dt1 >> csv1
sql2 >> dt2 >> csv2
sql3 >> dt3 >> csv3

1 Like

Buddy @CScruggs
once you get the datatable from query activities
kindly do the following buddy

  1. inside the for each loop once after running the query and getting the output, use a write csv file with this type of file name
    “filepath”+now.ToString(“hh_mm_tt”)+“.csv” like this buddy

for example @CScruggs

“C:\Users\pp5128\Documents\UiPath\forum_2\csv\Samplefile”+now.ToString(“hh_mm_tt”)+“.csv”

this would create new csv file everytime with time stamp so that it would get created in different csv files
Try and let know whether this works or not buddy
Cheers @CScruggs

1 Like

Thank you Palaniyappan, that worked! Is there a way I can number each file (1,2,3, etc.) including the date and time?

1 Like

yes buddy @CScruggs instead of now.tostring you can use a counter of default value as 1 in a assign activity and increment it once after the query gets executed like this

“filepath”+“_”+counter.ToString+now.ToString(“hh_mm_tt”)+“.csv”

like this buddy
“C:\Users\pp5128\Documents\UiPath\forum_2\csv\Samplefile” +“_”+counter.ToString+now.ToString(“hh_mm_tt”)+“.csv”

@CScruggs

Cheers

Did that work buddy @CScruggs

1 Like

@Palaniyappan it should work. You gave the right instructions

1 Like

Yes, it worked! thanks again for your help!

1 Like

Buddy @CScruggs
If this gets resolved mark the right comment with solution buddy that could help others as well buddy
Cheeers

I thought I had this correct, but something is not right…

Check that counter variable type. You declared it as int array instead of int. @CScruggs @Palaniyappan

1 Like

Thank you. I changed the counter variable to an integer and all files got written to a separate csv file.

csvfiles

1 Like

Counter is a increment been generally used to depict a integer variable buddy, and as you were using array, it was showing error…
@CScruggs

My god…hehe…my network is this bad…i entered the previous comment 10 mins before but showing now only…
Alright…
Cheers buddy @devKarthikeyanR
Keep going… @CScruggs

Is there a way the .csv output file can pick up the name of the .sql file? Hope this makes sense.

For example.
1.Sup.sql
1.Sup.csv
2.Mgr.sql
2.Mgr.csv

NO BUDDY ONLY CSV @CScruggs

right, not the extension, but the actual name of the file before the extension.

well you can get the csv file alone in exact like this
out_file_csv = Directory.Getfiles(“yourfolderpath”,“*.csv”)

This would work for you buddy
Kindly try this and let know buddy @CScruggs
Cheers

yes, that’s not what I am wanting to do. I just want to be able to give each .csv file a specific name when it is being written, so I know which datatable is which because each has a different set of data.

Can I have the .sql files run in numerical order?

yes buddy you can use
execute query activity from uipath.database.activities package
where you can pass the sql text read bfore
@CScruggs