How to get the exact file names?

I am using UI path for my development. Have few clarifications below .

  1. I am trying to get the File name exactly from the path mentioned. Say for example, 3 pdf files are in “C:\users\kk\hi.pdf”, “C:\users\kk\welcome.pdf”, “C:\users\kk\readme.pdf”. I tried to get the PDF names using the foreach item after providing the path using “Directory.getfiles(selectedfolder)”. I am trying to get the file name exactly to pass for the next condition. like, i just need the names of it “hi.pdf”,“welcome.pdf”,“readme.pdf”.

2.I need to pass the exact pdf in to the getpdf activity to scrap few elements? Any suggestions on whats the best method?

  1. After extracting, i need to update the existing excel sheet (not a new one)… Any help on this? suggestions are most welcome. Appreciate your help.
1 Like


Directory.GetFiles(folder) or System.IO.Directory.GetFiles(folder) will give you the array of filepaths which you can loop through. So, that should be enough for your GetPDF activity cause the full filepath will tell it to use that file.

However, if you would like to only get the filename you can use Path.GetFilename(fullpath) or System.IO.Path.GetFilename(fullpath). So, in a ForEach file In Directory.GetFiles(folder) loop you could do Path.GetFilename(file) to get the filename only.

Like I said though, Directory.GetFiles(folder) should give you the fullpaths that you can use in read activities. Unless you need to change the folder location then use it with .GetFilename()




Thanks @ClaytonM for your time. I am trying to get the file name to redirect for another activity as well… I have written another sequence to get the full path on reading activity.

But, on getting file name task… i tried your suggestion (instead of Directory.getfiles(folder), i have used the path.getFilename(file). which gives me an error on converting to array file as the assigned file is Array of string.
Am i making anything wrong here?

Also, i tried to pass the full path in to foreach and tried to get the filename inside the for each where am getting the error " For each file : Unable to cast object of type ‘System.Char’ to type ‘System.String’."


1 Like


Sorry, let me clarify.
Directory.getfiles(folder) gives you the array of filepaths. for example, you can do Directory.getfiles(folder)(0) and it will give you the first filepath in the array.

The best way to go through the array is with the ForEach. You first need to change the Type Argument in the properties to String.
ForEach f In Directory.getfiles(folder)
Assign file = f.ToString

f can be any word of your choosing to be used as the variable in the ForEach
So to get only the filename you can do it like this:
ForEach f In Directory.getfiles(folder)
Assign filename = Path.GetFilename(f.ToString).ToString

where file and filename are both String variables if you choose to store it first.

Remember to change the Type argument to String.
If you still having problems, you can post a snippet of your .xaml or screenshots and maybe I can spot the error.



Main.xaml (6.3 KB)
project.json (238 Bytes)

Please find the attachment. You can remove the folder name inside for each to get only the file name.
Hope you got only the file name inside the directory.



Hi @ClaytonM - I tried putting up your approach and getting a cast error.

since am new user, its not allowing me to upload xaml here.i have uploaded a screenshot here. please correct me if am wrong.

Thanks @Girish. I tried this approach earlier (the one which u have sent)… It actually gives the output of the whole path and not the file name exactly.

I see you are replacing the folder path with " ", but thats not working.


I checked the output it is working, could please pass the folder path properly and give space in double quote as " "?

Looking over your screenshot, you need to remove the 2nd “For each” there, cause you only need 1.

Just change the Assign activity to this:
fileName = Path.GetFilename(file)

Then use fileName in a MessageBox.



@Girish - Awesome, it worked. i havent changed the folder path earlier … my bad. thanks.
Do you have any idea on the other 2 points which i asked for?

@ClaytonM - Thank you so much again for your time. It worked again with the corrections.
If you get a chance, can you please provide me any suggestions on the other two points i put up?

Hi Virags,

For other steps you can perform operations as explained below:

  1. Rename the file and move into another folder and take that file and pass particular PDF from moved folder inside the for each, so you can use counter to rename the file and get that file using the same name.

  2. You can perform the “Read PDF” and do some string manipulation on that PDF and get values using string operations like split.

3)Pass the extracted values into your excel sheet using the write cell activity.

Hope you will go through steps as explained above.


Hi @Girish

Thank you so much for your steps. I was able to do the first step. in the second step, i am not able to Read the PDF with the scrap (for multiple pages), I could able to extract data using the “ReadPDFText” but, the tabular data values in the pdf is not well aligned (as in, the left side of the tab comes in the first set of file and the right side of the table comes below of that).

Any other way to read the pdf exactly? (i tried with Anchor base, selectors and Read pdf with ocr as well).
Appreciate your help.


If you want few elements to be extracted from the scraped table then no need to worry about the alignment. You can perform string manipulation to get the values and assign it to variable in the UIPath studio.


Yes you are right, But the issue is it is 4 page document with the tables. When the scraped table is converted to a text file… the 4 page document entirely loses its tabular format. it is actually getting difficult in performing the string manipulation in this situation.

I am looking forward to extract the exact table format in to a excel or something which would be helpful.
Hope you got my problem? Am i missing something here?


1 Like

Hi this is will give some idea to u


I am creating a folder by "sender_mail_address / mail_date " .
In Mail date subfolder I save the PDF attachment.
I want to read that PDF name.
Can you please give me suggestions?

Hi @Jayesh_678

Each mail message has the attachments stored in an array of MailMessage.Attachment type. In order to get the attachment name, you can use .Name.

Since it’s an array, you need to either hardcode the index, like mailmsg.Attachments(0).Name
Ideally, you would run those through a loop.
For example,

For each attch In mailmsg.Attachments  // typeargument: Attachment
    assign attachName = attch.Name

For debugging, you can output all the attachment names using some vb
WriteLine String.Join(",", mailMsg.Attachments.Select(Function(x) x.Name) )

Hope that helps.


Actually this attachment is stored in “D:/ Mail_addaress /current_date” .
From that current date I just want to fetch the file name. There is only one file.

oh ok.
So you can use Directory.GetFiles() to get all files in that folder, whether it’s one file or not, it will still give you a list of files. But, you can hardcode the index as 0 if you only want to do the first one it finds.

I don’t recommend using local locations for data storage, unless it’s strictly for attended. But, I’ll use your path as a variable. Example shown below:

srcPath = "D:/ Mail_addaress /current_date"
srcFiles <Array Of Strings> = System.IO.Directory.GetFiles(srcPath, "*.pdf")

For each f In srcFiles
    Read f

That’s just pseudocode to represent the logic needed.

If you don’t want to run a loop through each file it finds, then hardcode the index, like srcFiles(0), or even sort it and take the most recent file like srcFiles.OrderBy(Function(x) System.IO.File.GetCreationTime(x) )(0)

EDIT: added extension pattern to GetFiles() so it only finds pdf files.

Hope that answers it better.


1 Like