Go through every folder and copy each file name into a excel list

Good day,

I am writing a program that should go through a folder with 20 other folders. It should open every folder in which are files, these file names should be copied into a separate excel list. Can you help me with this program?

Kind regards
Artem

1 Like

Hi welcome to the community!
Use Buid Data Table
Create a variable of type String() and assign to this Directory.GetFiles("C:\start","*",SearchOption.AllDirectories)
Create a For Each and iterate that variable
Add rows in the table using item from the loop
Use Write range to create excel

5 Likes

Hi,

It works! But i have a little problem… the program should open just the first folder level not the next too… have you any clue how i can do this?

1 Like

It should open each of this folders:image
and just copy the file / folder names in it.
But it also opens the folders inside and get the names of the others files.

you can change the search option to SearchOption.TopDirectoryOnly to get the list of the 20 folders inside and then for each of the folders you can do the same search option to get the files in each of the folders while appending all the file name into the same datatable variable and finally write it into an excel.

GetDirectories(SearchOption.TopDirectoryOnly)

then for each sub-directory

GetFiles(SearchOption.TopDirectoryOnly)

2 Likes

How would it be in my code?


I am using
Directory.GetFiles(“N:”,"*",SearchOption.AllDirectories)
For the for each interation.


Some thing like i have attached
Sequence.xaml (9.0 KB)

1 Like

Many thanks for your code!
But I have a little Error: image

And I only started today with UiPath.

1 Like

are you running my sequence?

yeah

Well your issue is simple. The max length of the column is set to 100 in my BuildDataTable.
Increase it to a number that you think is suitable.
Also i have made a modification that gets only the name of the file and not the whole path.

Check this updated sequence
Sequence.xaml (9.0 KB)

2 Likes

Many thanks for your code!
I don´t get the error anymore.
But how can I see the excel sheet now?

In my sequence i had not added the excel section to write the file.
See this updated one. You can change the file path if you like to the path you desire.
Sequence.xaml (10.2 KB)

Once you execute the file will be in the location you have selected.

2 Likes

Many thanks it works excellent!

1 Like

Can you help me on this one too?

Now I am working on my second one…
I have a little problem…

My robot should open an excel application and copy each entry and search in different folders for the word, if it finds an entry it should copy the file with the name into a other places.
Can someone help me here out?

The excel file looks like this: image

And it should start the search in the first folder: image
And if it finds nothing, it should search in the next one.
But if it finds an entry with the name then it should copy it into an onther folder.

I hope someone can help me.
Many thanks in advance!

1 Like

It should be an extension of what you have already prepared. Just additional logic is required in it.
Let me see if i can send you something.

1 Like

Here you go. I added it to the same sequence which i have shared before.
Modify as required
Sequence1.xaml (14.7 KB)

I am using your code.
But I get a failure message:

In your excel sheet what is the name of the column?

I have used LookupDataTable activity which searches for the file name in the datatable(your name excel) based on the column name. If found it will return a positive number else -1.

If you dont have a column name then you can remove the property for that activity.

let me know if you have any questions.

1 Like

I could not eliminate the error.