UiPath- To check if a file is Excel or CSV, and opens the file.

ray sha
5 min readSep 28, 2020

--

INPUT

Input: An excel file and a CSV file

Name: Input_Excel.xlsx

Input_CSV.csv

Adding a screenshot of the files.

OUTPUT

Output: Checks the extension and reads the files.

I’ve added a message box, which shows which file is being read.

If CSV file is read:

If Excel files is read:

FLOW

To achieve the desired output, please follow the below steps:

1. Open UiPath Studio. Select “Process”.

2. Create a new process by entering a valid name and change the description to something meaningful. Location must be the path where you want to save the process.

3. From the activities panel, select a sequence or a flowchart. I’m starting with a sequence. Rename the sequence to something meaning. Add annotations.

4. Select an “Assign” activity. The contents of this activity is:

To: Files

Value: Directory.GetFiles(“{Path to the directory which contains the two files}”)

Where:

Files is a variable. To create a variable go to the variable panel and create one or use ctrl+K.

Variable type: Array of strings.

Scope: Extend it to the whole scope

The screenshot of the assign activity

The screenshot of properties panel for the assign activity

5. After the assign activity, use a “for each” loop

6. The contents of for each are:

TypeArguement: String

Value: Files

7. In the body of for each loop, use an “if condition”

8. The contents of “if” is:

Condition: Path.GetExtension(item)=”.xlsx”

This checks if the file is an Excel file, if true, Then loop is executed, otherwise Else loop will be executed.

9. In the Then Part, add Message Box with text — “Excel file present”.

10. Below the “Message box” activity, use an Excel Application scope, in the “do” part use a Read range activity. Be careful here, there are two Read range activities, One for Excel and the other for workbook. If you use the Workbook’s Read Range, then do not use the excel application scope. Since we are using Excel Application Scope, use Read Range for Excel.

The properties of Excel Application Scope:

Path: item

11. Below message box activity, use a Read Range activity. The properties of this activity are:

Has Headers: True

Data Table: excel_dt

12. In the Then part, add an If activity with condition as

Path.GetExtension(item)=”.csv”.

13. In the Then part of the second IF activity, add a message box activity with text- “CSV file present”

14. Below message box activity, use a Read CSV activity. The properties of this activity are:

File path: item

Delimiter: Comma

Has Headers: True

Data Table: csv_dt.

15. In the Else part of the second IF activity, use a Write Line activity which writes the output to the console stating- “Not a csv or excel file”.

I really hope this solves your doubt. Feel free to reach out to me for any further queries.

Always remember to be kind.

All the best and Happy learning!

--

--

ray sha
ray sha

Written by ray sha

An effort towards making myself happy!

No responses yet