activities
latest
false
UiPath logo, featuring letters U and I in white

Productivity Activities

Last updated Dec 18, 2024

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:

  1. 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
  3. 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.
  4. 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.
  5. Drag a Sequence container and connect it to the previous Read CSV activity.
  6. Double-click the Sequence to open it.
  7. 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.
  8. 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

      N/A

      selectedRow

      IEnumerable<DataRow>

      N/A
  9. 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.
  10. 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.
  11. 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.
  12. 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.
  13. Return to the Flowchart.
  14. 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.

Was this page helpful?

Get The Help You Need
Learning RPA - Automation Courses
UiPath Community Forum
Uipath Logo White
Trust and Security
© 2005-2024 UiPath. All rights reserved.