Help with coding

In a workflow named “Khorus Reminders” that I am working with, I have created two variables (last_mod_file and directory) to store a file path. I am working with a system that downloads a report to my Downloads folder. I want the bot to read the last modified file in that location. I used an Assign Activitiy:

directory = new DirectoryInfo("C:\Users\jamesb\Downloads")

last_mod_file=directory.GetFiles.Where(function(file) file.LastWriteTime.Date= DateTime.Now.Date).OrderByDescending(function(d) d.LastWriteTimeUtc).ToArray(0)

What I expect to happen is that the variable last_mod_file will store the file name of the last modified file in my Downloads folder. I end up getting the following message:

Read CSV: Could not find file ‘C:\Users\jamesb\Documents\UiPath\Khorus_Reminders\user list by compliance report 4q 2018 (11).csv’.

It is successfully finding the file but for some reason it is inserting “UiPath\Khorus_Reminders” into the file path. Do you know why this is occurring? I can’t figure out how to get the correct file path in the last_mod_file variable.

Hi @jberrocales,

Have a variable for the filePath = “C:\Users\jamesb\Downloads”
Another suggestion is you might want to change the variable name from directory to something more meaningful. Since “directory” is a keyword.

Getting the last modified file returns just the file name and then when you pass last_mod_file Read CSV activity requires a full Physical path, hence it is appending the project folder path to it and you are seeing the “Khorus Reminders”.

Therefore on the Read CSV activity, please combine the path and the last modified file name:
Path.Combine(filePath,last_Mod_File.ToString)

Happy Automating!

Regards,
PD

I do not see any issue with the existing code.
Worked with the following xaml -

Main.xaml (6.0 KB)

1 Like

Agreed. Directory is found in System.IO.Directory, therefore a different variable name is recommended.

I agree that it must be using just the filename, so it sees it as the relative path and uses the project directory. However, doesn’t .GetFiles() return the full filepaths? I’m fairly sure it does so his code that pulls in the last modified file should be the full filepath, unless I’m mistaken. Then, maybe at some other point in the code he is using a .GetFileName(). :man_shrugging:

I would normally set it up like this:
Directory.GetFiles("C:\Users\jamesb\Downloads", "*").Where().OrderByDescending().ToArray.First
And, fill in the missing code.

Maybe the method used with DirectoryInfo() returns a different result. So you can try the method I suggested. @jberrocales

1 Like

Hi @ClaytonM,

You’re right there are multiple ways to arrive to the solution. The user already started with DirectoryInfo class, I was helping him identify the solution thru his approach.

GetFiles() method from DirectoryInfo class returns an array of FileInfo type. And when I tried it out I got the relative path and not absolute path. Hence combining the file path was required at Read CSV activity.

Leveraging the FileInfo properties “LastWriteTime” to get the last modified file name; pretty much the same code:
dr_Info.GetFiles().OrderByDescending(Function(o) o.LastWriteTime).First

1 Like

Thank you.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.