How to get the exact file names?

studio
control_flow

#1

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.

#2

Hello,

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()

Regards.

C


#3

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’."

Thanks,
KK


#4

Hi,

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.

Thanks.


#5

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.

Thanks
Girish


#6

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.


Wildcards in source path - UnZip File
#7

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.

Thanks,
KK


#8

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


#9

Hi,
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.

Regards.


#10

@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?


#11

@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?


#12

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.

Thanks
Girish


#13

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.

Thanks,
KK


#14

Hi,
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.

Thanks,
Girish


#15

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?

Thanks,
KK


#16

Hi this is will give some idea to u