For Each File in a Folder

I am using for each file in a folder activity to iterate through .crv files and whenever I use (CurrentFile) in any activity as CurrentFile.FullName, it gives me an error that it is workbook application. I also tried debugging and I saw that CurrentFile enters the do container as Null. Does anyone know why is that happening and how can I fix it. I just want to iterate though these crv files and put all of them in a new Excel file, each file in a sheet.

Hi,

Can you share your current workflow and screenshot when the error occurs, if possible?

Regards,

@Menna_Khaled

can you show some screenshots also arethere any two loops or so? or did you create a variable with ame currentfile? ideally in for loop you need not create

cheers

no I just used the automatically created (Current Item)

Yes, sure

Hi,

It seems CurrentFile’s type is illegal.
If you define variable named CurrentFile, can you remove it in DataManager or Variable panel?

Regards,


Depending on what version you are using it should be “CurrentFile.FullName” not
“CurrentFile.FilePath” Do you have a variable that’s named FilePath maybe ?
If so don’t use it. Use FullName instead.

Also I see your using excel application scope which is not needed if its a CSV.
You can just use Read CSV like seen below. I have the contents of the CSV outputted just to show it works.

@Menna_Khaled

as mentioned above looks like currentfile is a created variable

in locals panel can you show all your variables please

cheers

@Menna_Khaled

On further thinking you can also put an if statement in to prevent some errors that may come up like the CSV being empty. I’ve created one for an example.

If you were wondering CSVRange is a variable data table that is outputted in Read CSV.

I don’t have another variable with the same name.

I tried CurrentFile.FullName as well and it gave me the same error. currentfile also appears as workbook application but it is supposed to be FileInfo. It actually worked with me as CurrentFile.FullName first but then it gave me an error that is why I tried to make some changes.

Hi,

Can you share your workflow as attached file, if possible?

Regards,

Have you put the CSV filter in ?
image
Try putting a message box or a log to command to see the output or you can use a breakpoint in debug mode and use the immediate panel to check the variable value.
You can also try and go to Manage packages and make sure the packages are up to date.
Also remember CSV’s don’t need a Excel Application Scope. You can use read range or read csv activity depending on version. Here’s my version if it helps.
LoopThroughCSVsInFolder.xaml (14.1 KB)

I solved it by changing CurrentFIle to Fileinfo in an assign, but I will also check the package as it might be an old version and check your version to know how it should done exactly.. Thank you so much for your support.

I solved it by changing CurrentFIle to Fileinfo in an assign. Thank you so much for your support.

Hi @Menna_Khaled

UiPath has multiple “For Each File”–type activities, and two of them behave very differently:

A. For Each File in Folder (Modern) → OUTPUT TYPE = UiPath.Excel.WorkbookApplication**

  • This activity is primarily meant for Excel files, CSV, XML, etc.
  • It automatically tries to open files using the Excel integration layer.
  • .crv is not an Excel-supported format → the activity treats them as workbook applications → fails → CurrentFile = Nothing

B. For Each File (from System.IO.Directory) → OUTPUT TYPE = String

  • This version gives clean file paths.
  • No auto-opening, no Excel interference.
  • Works with any file extension including .crv.

You most likely used A, which is the root cause.

Use Standard For Each + Directory.GetFiles**

Step-by-step fix

1. Drop a regular For Each Activity

  • Type Argument: String

2. Set Items to:

Directory.GetFiles("C:\Your\Folder\Path", "*.crv")

3. Now you can safely use:

  • item → gives full file path
  • Path.GetFileName(item)
  • Path.GetFileNameWithoutExtension(item)

All without Excel interference.

If you want each CRV file converted into a sheet in an Excel file

You can do something like:

Inside the For Each:

  • Read the .crv content (if applicable)
  • OR convert .crv to PDF/Excel if you have a converter
  • OR simply log the file name into a specific row/sheet

But if .crv is a binary Crystal Reports file, UiPath cannot read its contents directly without:

  • Crystal Reports runtime
  • Export options
  • Or a library to convert it from CRV → Excel/PDF