Compare CSV Files
The example below explains how to create two new .csv files that store the similarities and the differences between two existing .csv files. It presents activities such as Read CSV or Build Data Table. You can find these activities in the UiPath.Excel.Activities package.
Create a new CSV file with the differences between two tables
This is how the automation process can be built:
- Open Studio and create a new Process and name it Differences.
note
Make sure to add the .csv files that you want to use in the project folder. You can also use the files we provided by downloading the example available at the bottom of this page.
:::
2. Drag a Flowchart container in the Workflow Designer.
- Create the following variables:
| Variable Name | Variable Type | Default Value |
|---|---|---|
Data1 | System.Data.DataTable | N/A |
Data2 | System.Data.DataTable | N/A |
newRow | System.Data.DataTable | N/A |
-
Place a Read CSV activity in the Flowchart panel.
- Right-click the Read CSV activity and select the Set as Start Node option.
- In the Properties panel, add the path of the
csv1.csvfile in the FilePath field. - Select the IncludeColumnName check box.
- Add the variable
Data1in the DataTable field.
-
Add a new Read CSV activity and connect it to the previous one.
- In the Properties panel, add the path of the
csv2.csvfile in the FilePath field. - Select the IncludeColumnName check box.
- Add the variable
Data2in the DataTable field.
- In the Properties panel, add the path of the
-
Drag a Sequence container and connect it to the previous Read CSV activity.
-
Double-click the Sequence to open it.
-
Drag a Build Data Table activity to the Sequence.
- In the Properties panel, add the variable
newRowin the DataTable field. - Click on the DataTable button, inside the body of the Build Data Table activity. The Build Data Table window contains a table with two columns and rows.
- On the left column, click on the Edit Column button and add the name
Numberin the ColumnName field. - Set the Data Type as Int32.
- Select the OK option for closing the window.
- On the right column, click on the Edit Column button and add the name
Characterin the ColumnName field. - Set the Data Type as String.
- Select the OK option for closing the window.
- Select the OK button again to close the window.
- In the Properties panel, add the variable
-
Drag a For Each Row activity under the Build Data Table activity.
- In the Properties panel, add the variable
Data1in the DataTable field. - Select the Body sequence and create the following variables:
Variable Name Variable Type Default Value strCurrentNoString N/A selectedRowIEnumerable<DataRow> N/A - In the Properties panel, add the variable
-
Place an Assign activity in the body of the For Each Row activity.
- Add the variable
strCurrentNoin the To field. - In the Properties panel, add the expression
row("Number").ToStringin the Value field.
- Add the variable
-
Drag another Assign activity and place it under the previous one.
- Add the variable
selectedRowin the To field. - In the Properties panel, add the expression
Data2.Select("Number ="+strCurrentNo)in the Value field. TheSelect(String)method is used to filter and extract the values that are equal in the two.csvfiles.
- Add the variable
-
Place an If activity below the last Assign activity.
- Add the expression
drSelectedRows is Nothing or drSelectedRows.Count=0in the Condition field. This condition checks if theselectedRowvariable is invalid or if the number of rows is 0.
- Add the expression
-
Drag an Add Data Row activity to the Then section of the If activity.
- In the Properties panel, add the expression
row.ItemArrayin the ArrayRow field and the variablenewRowin the DataTable field.
- In the Properties panel, add the expression
-
Return to the Flowchart.
-
Drag a Write CSV activity under the Sequence container and connect it to it.
- Enter a name for the file to be created in the FilePath field. In this case, the input is
“Differences.csv”. - Add the variable
newRowin the DataTable field. - Select the AddHeaders check box for considering the first row as headers.
Create a new CSV file with the similarities between two tables
This is how the automation process can be built:
- Repeat steps 1-10 from the Differences flowchart.
- The new Flowchart should be named Similarities.
- Drag an Add Data Row under the two Assign activities.
- In the Properties panel, add the value
row.ItemArrayin the ArrayRow field and the variablenewRowin the DataTable field.
- In the Properties panel, add the value
- Return to the Flowchart.
- Drag a Write CSV activity under the Sequence container and connect it to it.
- Enter a name for the file to be created in the FilePath field. In this case, the input is
“Similarities.csv”. - Add the variable
newRowin the DataTable field. - Select the AddHeaders check box for considering the first row as headers.
- Enter a name for the file to be created in the FilePath field. In this case, the input is
- Save the automation project.
- Run the process. The automation process should filter the data and create two new files, one with the differences and one with the similarities between the two initial files.