UiPath Activities

Compare CSV Files

The example below explains how to create two new .csv files that can store the similarities and
the differences between two parent .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:

  1. Open Studio and create a new Process and name it Differences.

Important!

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.

  1. Drag a Flowchart container in the Workflow Designer.
    • Create the following variables:
Variable Name
Variable Type
Default Value

Data1

System.Data.DataTable

-

Data2

System.Data.DataTable

-

newRow

System.Data.DataTable

-

  1. 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.csv file in the FilePath field.
    • Select the IncludeColumnName check box.
    • Add the variable Data1 in the DataTable field.
  2. Add a new Read CSV activity and connect it to the previous one.
    • In the Properties panel, add the path of the csv2.csv file in the FilePath field.
    • Select the IncludeColumnName check box.
    • Add the variable Data2 in the DataTable field.
  3. Drag a Sequence container and connect it to the previous Read CSV activity.
  4. Double-click the Sequence to open it.
  5. Drag a Build Data Table activity to the Sequence.
    • In the Properties panel, add the variable newRow in 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 Number in 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 Character in 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.
  6. Drag a For Each Row activity under the Build Data Table activity.
    • In the Properties panel, add the variable Data1 in the DataTable field.
    • Select the Body sequence and create the following variables:
Variable Name
Variable Type
Default Value

strCurrentNo

String

-

selectedRow

IEnumerable<DataRow>

-

  1. Place an Assign activity in the body of the For Each Row activity.
    • Add the variable strCurrentNo in the To field.
    • In the Properties panel, add the expression row("Number").ToString in the Value field.
  2. Drag another Assign activity and place it under the previous one.
    • Add the variable selectedRow in the To field.
      • In the Properties panel, add the expression Data2.Select("Number ="+strCurrentNo) in the Value field. The Select(String) method is used to filter and extract the values that are equal in the two .csv files.
  3. Place an If activity below the last Assign activity.
    • Add the expression drSelectedRows is Nothing or drSelectedRows.Count=0 in the Condition field. This condition checks if the selectedRow variable is invalid or if the number of rows is 0.
  4. Drag an Add Data Row activity to the Then section of the If activity.
    • In the Properties panel, add the expression row.ItemArray in the ArrayRow field and the variable newRow in the DataTable field.
  5. Return to the Flowchart.
  6. 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 newRow in 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:

  1. Repeat steps 1-10 from the Differences flowchart.
  2. The new Flowchart should be named Similarities.
  3. Drag an Add Data Row under the two Assign activities.
    • In the Properties panel, add the value row.ItemArray in the ArrayRow field and the variable newRow in the DataTable field.
  4. Return to the Flowchart.
  5. 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 newRow in the DataTable field.
    • Select the AddHeaders check box for considering the first row as headers.
  6. Save the automation project.
  7. 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.

 
 
Download example


Compare CSV Files


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.