Find Specific File in the Directory and Return The File Name in the Email

Hello,

I need assistance in looking for a specific file name in a directory and if the file is found, I need to return the file name and be sent to my outlook email.

I was able to figure out how to look for specific file name by using below (This will find any file that starts with EPICTRAN_ … with today’s date, so example, EPICTRAN_20241011101358930). I am expecting a file with similar naming convention daily.

Below Script Worked:

System.IO.Directory.GetFiles("\epic-nas.et1157.epichosted.com\xfr\prd\toODB\Claims\PBClaims\CRD\archive",“EPICTRAN_”+Now.ToString(“yyyyMMdd”)+“*.txt”).Any()

The issue I am having is, how do I get the file name that was found from above script and then return that and send to my outlook email. I dont know how to save the result of the file that is found thats where I am struggling.

@nirmit.kansagra

Use an assign activity…create a string type variable

And if the above expression returnes true then instead of .Any use

Strvar = yourexpression.First

Cheers

Do you think you can share what you mean this? Sorry I am not the best at coding so was hoping if you can share exactly what you mean? Right now I am using If else statement and in IF i am using the condition System.IO.Directory.GetFiles(“\epic-nas.et1157.epichosted.com\xfr\prd\toODB\Claims\PBClaims\CRD\archive”,“EPICTRAN_”+Now.ToString(“yyyyMMdd”)+“*.txt”).Any() is true then send outlook email stating file found, else send email on outlook stating false.

@nirmit.kansagra

Use assign

Strvariable = System.IO.Directory.GetFiles(“\epic-nas.et1157.epichosted.com\xfr\prd\toODB\Claims\PBClaims\CRD\archive”,“EPICTRAN_”+Now.ToString(“yyyyMMdd”)+“*.txt”).first

This will save the path to string. Variable

Then use send outlook mail message activity

And pass the string variable

If you dont find send outlook mail message go to manage packages and install UiPath.mail package

Cheers

Thank you so much this. How do I send an email if my file is not found? I want to send email for both cases if found or if not found. I got the email to be sent with the file name if file is found, but don’t know how to send email if not found.

1 Like

@nirmit.kansagra

On else side use send email again and give body as not found

Cheers

You can use an if activity with the condition using the following expression:
System.IO.Directory.GetFiles(“\epic-nas.et1157.epichosted.com\xfr\prd\toODB\Claims\PBClaims\CRD\archive”,“EPICTRAN_”+Now.ToString(“yyyyMMdd”)+“*.txt”).any()

For True, you can store the filepath in a string variable using the expression:
System.IO.Directory.GetFiles(“\epic-nas.et1157.epichosted.com\xfr\prd\toODB\Claims\PBClaims\CRD\archive”,“EPICTRAN_”+Now.ToString(“yyyyMMdd”)+“*.txt”)(0)

In the else block, you may write logic for file not found

Hi, @nirmit.kansagra.

Could you please the steps below:

Step 1:
This image shows a workflow sequence that assigns the value "teste3.txt" to the variable  and then initiates a loop to iterate through each file in a folder. (Captioned by AI)

Step 2:
This image shows a workflow configuration for iterating over each text file in a specified directory, sorting by most recently created, and assigning each file's name to a variable. (Captioned by AI)

Step 3:

strFileNameWithoutExtension = Path.GetFileNameWithoutExtension(strFile)

Here is the code:
FindFileName.zip (3.7 KB)

Hi @nirmit.kansagra,

you can get the file name with:
System.IO.Directory.GetFiles(“\epic-nas.et1157.epichosted.com\xfr\prd\toODB\Claims\PBClaims\CRD\archive”,“EPICTRAN_”+Now.ToString(“yyyyMMdd”)+“*.txt”).First.ToString

by replacing “.Any()” at the last with “.First.ToString

Just make sure you use this in the then of your If condition as any similar solution will throw exception if the file is not found

Hie @nirmit.kansagra you can pass the send outlook activity in both condition and as per @Anil_G told you if the condition is true attach the file in the then box . and if not in the else section use send outlook activity and write the mail whichever you want to send …

This image depicts a flowchart with a conditional "If" statement that, based on the condition, either sends an email with an attachment or sends an email without an attachment. (Captioned by AI)

cheers Happy Automation.