Attach file (Outlook) with unknown name and path

Hi all,

Sorry, a bit of a rookie question here. I have searched both on the forum and Google, but can’t find any answer to this issue.

I’ve set up an automation which goes into Salesforce and downloads a daily report. The report always saves with a new random file name (report + random number.xls). I want to create an automation which picks up the daily report, attaches it to an e-mail and then sends it off to a number of people.

Because I don’t know the file’s name before the automation starts, I’ve set it up to look through the folder where the report is saved and then pick the newest file (I got help to do this from a different thread on this forum). The way I do this is by:

Assign: directory = new DirectoryInfo(“C:\Users\username\Downloads”)


Assign: last_mod_files = directory.GetFiles("*.xls").Where(function(file) file.LastWriteTime.Date= DateTime.Now.Date).OrderByDescending(function(d) d.LastWriteTimeUtc).ToArray


Assign: MyExcelFile = last_mod_files(0).ToString

When I try to print MyExcelFile to the console it returns the correct filename. So far, so good. However, my issue is that I don’t know the next step from MyExcelFile = last_mod_files(0).ToString to actually getting that file attached to an e-mail.

I know the Outlook activity and how to make UiPath attach a given file if you know the file path and name before the automation starts. But in this case I can’t put in the file path before the automation runs, because the file doesn’t exist before the automation is run.

I hope someone can help me with this.

Thank you!


You have to pass MyExcelFile variable to attachment section under Outlook mail.

Hi @jakobk,
This situation can be handled in different ways,

  1. Save the file as you did now.
    Get the file by Directory.GetFiles(“folderPath”)(0).toString
    Pass this as the attachment file path in outlook acivity and send
    Then Move the file to another location, so that duplicate issue won’t happen next time.

2)Hard code the path to the download folder, then append the name you got from the above steps

3)you said you can get the file name from the above steps, then get its Index position in array using IndexOf() method, then pass last_mod_files(the index we got).tostring

Try. Good luck

Welcome to uipath community
You were almost done
Use a assign activity like this
str_filepath = Directory.GetFiles(“yourfolderpath”,”*.xls”).orderbydescending(Function(a) New FileInfo(a).CreationTime).ToList(0).ToString

—now the string variable str_filepath will have the latest file created
—After obtaining this variable with value then use SEND OUTLOOK MAIL ACTIVITY with attach file mentioned as str_filepath.ToString

So the thing we need to ensure is we need to get the file created latest from the folder and then send the file

Hope this would help you
Cheers @jakobk

1 Like

Hi all,

Thank you very much for your replies.

@Palaniyappan When I do the following step:

Assign: str_filepath = Directory.GetFiles(“myfolderpath”,”*.xls”).orderbydescending(Function(a) New FileInfo(a).CreationTime).ToList(0).ToString

I receive the following error message:

Option Strict On disallows implicit conversions from ‘string’ to ‘System.IO.SearchOption’

Not sure what I’m doing wrong here. The variable “str_filepath” is created as a string variable.

Also, is it correct that the step you suggest (str_filepath = Directory.GetFiles(“myfolderpath”,”*.xls”).orderbydescending(Function(a) New FileInfo(a).CreationTime).ToList(0).ToString) makes my other 3 steps above obsolete, or should I insert your step after Assign: MyExcelFile = last_mod_files(0).ToString

I hope that makes sense.

Thank you very much for your help all!