How to get the extension of a mail attachment file

Hey everyone,

I have a process where I save attachment file of .pdf .xlsx .xls .csv , and I save the attachments with “save attachments” activity, then I “move file” to rename the saved attachment.

BUT, to move file with a new name I also hard code it as : director+“/”+newname+“.pdf”

I save it like this, but I dont want to hard code .pdf , I want it to be dynamic so I can save more than just a pdf. any suggestions?

1 Like

Hi @jntrk
You can either use FileInfo methods to get extension of the file

Alternatively, a quick and dirty option is to substring after the last index of dot ( . )
Assign → fileExtension = filename.Substring(filename.lastIndexOf("." + 1)

Read the extension and in your move file, add that extension instead of the hard-coded “.pdf”

Assign → fileExtension = filename.Substring(filename.lastIndexOf(“.” + 1)

I have tried this.

I got the path of the saved attachment, for ex: asdsa/ASDasdA/2020/test.xlsx and assigned it as filename(string)

then ı try to assign:
fileextension=filename.Substring(filename.LastIndexOf(“.”+1)) but i get the below error
image

any ideas?

Hi @jntrk
My apologies, the correct syntax is
Assign → fileExtension = filename.Substring(filename.lastIndexOf(".") + 1)

lastIndexOf(".") + 1 gives you string after the dot.
lastIndexOf(“.”) will yield you the extension including the dot e.g. “.pdf” or “.xlsx”
The +1 merely applies the substring one character later.

1 Like

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