How to Get CSV Files Only in Folder

Hi All,

How can i get CSV Files only in a folder that has a lot of file type?

Thanks :smiley:

4 Likes

Hello,

You will want to use the GetFiles member like Directory.GetFiles()
This allows you to use a wildcard for extension or parts of the filename
Google Directory.GetFiles

For example,
csvFiles = Directory.GetFiles(sourceDir, "*.csv")

This will give you an array of filepaths as strings

There might also be an activity for this, but I usually use the .net code so I can use it directly in any expression field within UiPath.

You can also set SearchOptions like to look in All subdirectories too
csvFiles = Directory.GetFiles(sourceDir, "*.csv", System.IO.SearchOption.AllDirectories)

I hope this helps.

Regards.

5 Likes

Hi @ClaytonM

Thank you for your swifltly reponse.Earlier i tried to use the Directory.GetFiles with out search options but i have no luck i got an error also when i tried the other example i got also an error.

Thanks :smiley:

4 Likes

Hi @pattyricarte
To get only Csv Files from The folder Use Directory.GetFiles(“YourFolderPath”, “*.csv”). It will gives you all Csv Files Which are present in That Folder.

1 Like

Hi @jitendra_123

That was also @ClaytonM suggestion but it doesn’t work at all.

Thanks :smiley:

7 Likes

@pattyricarte
This error is Occurring because the path you are giving Contains the Characters which are not allowed even you try to give the Path Manually so Replace or Remove that characters and retry it.
Thanks & Regards

1 Like

Hi @jitendra_123

I already try to replace some character.

but i got stuck with the error!

Thanks :smiley:

6 Likes

@pattyricarte
Just use write line in the for Each and see what Values are coming of the as Csv Files Path. so it will help you to understand.

1 Like

It might be pulling in the hidden temp file that starts with “~$” or whatever it starts with.

You will need to use an If activity to skip over the ones that start with “~”, or use linq to remove them from the list.

The .net solution would be like:
Directory.GetFiles(in_StrFilePath, "*.csv", System.IO.SearchOption.AllDirectories).Where(Function(f) Not f.StartsWith("~") )

4 Likes

Hi @ClaytonM

I tried your solution but got an error!

Thanks :smiley:

6 Likes

Your directory on in_StrFilePath must not be a directory. Can you validate that it is a folder path?

2 Likes

@pattyricarte
I think problem is that in Replace that you are used in Directory.Getfiles there you are replacing "\" with "\\".

2 Likes

Hi @ClaytonM, @jitendra_123

This is my codes below I also indicated the value of filepath

Thanks :smiley:

6 Likes

@pattyricarte
Replace "\\" with "\" may be it will resolve your Issue.

4 Likes

You can remove the double \\s, so it’s just single \. However, I don’t know if that resolves the issue, because usually you can use single or double; I am not sure though.

3 Likes

Hi @jitendra_123

I already did but i got no luck just stuck with the error!

Thanks :smiley:

6 Likes

Hi @pattyricarte

I have created workflow based on your requirement please run and check this will help you.

OpenCSV.zip (22.3 KB)

1 Like

Hi @ClaytonM, @jitendra_123, @kommijeevan

My apologies guys all of your solution are correct.I’m to naive that i assign also value in assign activity and also i assigned in defult value. Again apologies guys i don’t know how to give credits. But Many thanks for helping me out…

cheers :smiley:

Happy learning :smiley:

8 Likes

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