Extracting filenames from a folder and write filenames to excel

Hi All,

I am trying to get the contents and count of files from a folder and then write the names on to an excel file where I will be further refining the name to arrive at the data I want. I have 2 issues here for which I would love some help;

  1. I am saving the names of the files in a string array, how do I now convert this to make the bot write it in an excel? I have tried Build data table->for each->(body ->add data row)->output data table->write range. I get an error regarding column length(the method I’m using here is something I saw from an example someone posted here)
  2. Also is there a way for me to save the option clicked by the user in a select folder activity and call it for the subsequent activities for the aforementioned requirement? Or is pasting the path the only way to get the count and then the names of the files from the chosen folder ?
    I know I am doing something wrong here, any help will be great :slight_smile:

Thanks and Regards,
Saawan

You could run the array through a .Join() method, then use Write Text File to a .CSV file, which would result in a CSV file of your list of filenames.

Write Text File // to file.CSV
    String.Join(System.Environment.Newline, "Filenames", fileList)

I used “Filenames” as the header for the table but that can be omitted if needed.

The other way is to run the array through a loop, and use the Add Data Row activity

Build Data Table
For each file in fileList
    Add Data Row // ArrayRow: {file}
Write Range or Write CSV

I didn’t understand your other questions.

Regards.

Thank you Clayton, your tips have helped in progress, there’s one problem, the bot has iterated through the folder and has populated my excel only with the last file of the folder and not the others. What am I missing here ?

  1. What i was trying to ask help with in my previous message was:

At the moment, I have manually pasted the path of the folder I want the file iteration to be done on using an ‘Assign’ activity -> filelist = Directory.GetFiles(“C:\Users\user 1\Desktop\Test”)

Is there a way for me to use the ‘Select Folder’ activity and take the user’s chosen folder and that inturn dynamically fills in the path to arrive at the filelist.

Thank you :slight_smile:

That doesn’t seem right. You should check your filelist in a Message Box or Write Line to confirm you have all the files you want. You can do so like this: String.Join(",",filelist)

Also, make sure your table before you Write Range or Write CSV has all the items with either Output Data Table to a string or just output the Rows count like dt1.Rows.Count

I think so. You can store the selected folder to a string here:
image

Then, use that string in the .GetFiles() assignment.

Regards.

Hello @Saawan

Have you suceeded?
Are you able to get filename and write them in an excel?
If yes could you please share you xaml?
I would like to execute the same action :slight_smile:

Thank you very much!