i want to send the email with attachments in a single email, but there is always an extra attachment titled “~$n” that keeps appearing in the email attachment section, how can i remove this?
Follow this:
In UiPath, the presence of an attachment with a name starting with ~$ typically indicates that you are trying to attach a temporary file that is generated by applications like Microsoft Excel or Word when you’re editing a document. These temporary files are created to manage changes and they usually start with ~$ followed by the original file name.
To avoid sending this temporary file along with your email attachments, you can follow these steps:
-
Check the folder before sending: Before attaching files, ensure that you are only adding the intended files to your email. You can use the
Directory.GetFilesmethod to filter out files with the~$prefix. -
Filter file attachments: You can use a list of file extensions and filter the files based on the names, excluding any file that starts with
~$. Here’s how you can do this in UiPath:-
First, use the
Assignactivity to get the list of files from your directory:files = Directory.GetFiles("YourDirectoryPath") -
Then filter out the unwanted files:
attachments = files.Where(Function(f) Not Path.GetFileName(f).StartsWith("~$")).ToList() -
After filtering, use the list
attachmentsto attach files to your email.
-
-
Sending the email: Finally, when you use the
Send SMTP Mail MessageorSend Outlook Mail Messageactivity, pass the filtered list of attachments.
Here’s an example workflow:
// Get list of files from a specific directory
files = Directory.GetFiles("C:\path\to\your\files")
// Filter out files that start with ~$
attachments = files.Where(Function(f) Not Path.GetFileName(f).StartsWith("~$")).ToList()
// Prepare the email activity
Send SMTP Mail Message
- Attachments: attachments
- Subject: "Your subject"
- Body: "Your email body"
By implementing this filtering process before attaching files to your email, you should be able to avoid sending the unwanted ~$ temporary files.
i do not have the assign activity in my studiox profile. and how do i use the directory.getfiles method?
In UiPath StudioX, which is designed for business users and less technical users, certain programming concepts and activities, such as Assign, are not explicitly available. However, you can still achieve the desired functionality by using the built-in activities and a different approach to filtering attachments.
Steps to Exclude Unwanted Attachments in UiPath StudioX
-
Use the
Get FilesActivity: In StudioX, instead ofDirectory.GetFiles, you can use theGet Filesactivity to retrieve the list of files in a specified directory. -
Filter the File List Using the
UseFeature:- After retrieving the files, you might need to create a new sequence to filter the files before sending the email.
- While there isn’t an “Assign” activity, you can use a combination of the
Filter Data Tableactivity or a straightforwardIfcondition within a loop to check for file names.
-
Use
Send EmailActivity: Once you have filtered the files, you can use theSend Emailactivity to send the email with the valid attachments.
Example Workflow Steps
Here’s a suggested approach:
-
Get File Activity:
- Use the
Get Filesactivity to specify the folder where your attachments are stored. You can save the output to a variable namedFileList.
- Use the
-
For Each Activity:
- Drag and drop a
For Eachactivity to loop through the items inFileList. - Create a variable (e.g.,
ValidAttachments) of typeList<String>to store the valid attachments.
- Drag and drop a
-
Add an If Condition:
- Inside the
For Each, use anIfcondition to check if the current file does not start with the temporary file name pattern. - You can check if
Not currentFileName.StartsWith("~$"). - If the condition is true, use the
Add to Collectionactivity (which is available in StudioX) to add the valid file toValidAttachments.
- Inside the
-
Send Email:
- After the loop, use the
Send Emailactivity to send your email. - In the attachments property of the
Send Emailactivity, use theValidAttachmentsvariable that contains your filtered files.
- After the loop, use the
Example of the Steps in StudioX
-
Get FilesActivity:- Properties:
- Directory:
C:\Your\File\Path - Output:
FileList
- Directory:
- Properties:
-
For EachActivity (whereCurrentFileis the iteration variable):- Type argument:
String
Inside
For Each:IfActivity:- Condition:
Not CurrentFile.StartsWith("~$") - Then: Add to Collection (Add
CurrentFiletoValidAttachments)
- Condition:
- Type argument:
-
Send EmailActivity:- Set the
Attachmentsproperty toValidAttachments.
- Set the
Summary
@Sherrie1
By using the combination of Get Files, For Each, If, and Add to Collection activities in StudioX, you can filter out unwanted attachments effectively without needing the Assign activity. This method keeps everything reusable and functional within the StudioX UI framework.

