Hi team
I have a use-case that requires me to get a list of all the subfolders and the files present in the master folder and that master folder can either be on share point link or on local
so how can I create a list which proper details
sample input structure:
sample output structure:
![image](https://global.discourse-cdn.com/uipath/original/4X/b/5/5/b55565d5ba932eeae6412ab2bb02b9149ab88699.png)
Can anyone please help me out with this use-case?
thankyou ![:slight_smile: :slight_smile:](https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=12)
Hi @Aishwarya_Bhargava !
You can use the following function to extract all the subfiles from an original folder, independently if it is a local folder, or a synced sharepoint folder:
directory.GetFiles(var_FolderPath,"*.*",SearchOption.AllDirectories)
Expression description:
· var_FolderPath = Local path to extract files from.
· "*.*" = filter. If you'd like only PDF's for example, this would be "*.pdf*".
· SearchOption.Alldirectories = In order to look for files inside subfolders also.
This will not give you the desired structure you pasted on the 2nd screenshot, but will give you an array of strings, with the full path to each file:
Files found:
C:\Users\User\OneDrive - DemoFolder\Documents\\FolderA\FolderAA\FileA.txt
C:\Users\User\OneDrive - DemoFolder\Documents\\FolderB\FileA.txt
C:\Users\User\OneDrive - DemoFolder\Documents\\FolderB\FileA_1.xlsx
C:\Users\User\OneDrive - DemoFolder\Documents\\FolderC\FolderCC\FileCC1.txt
C:\Users\User\OneDrive - DemoFolder\Documents\\FolderC\FolderCC\FileCC2.pdf
With this list of final paths, you can manipulate the strings to get your desired DT output. But might be worth to leave it like this, if it suits your automation.
Other useful expressions you can use to extract filenames from this array of strings:
Path.GetFileName(var_arr_files(0))
-> This would return exactly "FileA.txt" from the 1st value in our example.
Best Regards,
Ignasi
Hi @Aishwarya_Bhargava,
Just as how @ignasi.peiris explained, you can use Directory.GetFiles to get all the files in a folder (including subfolders with SearchOption.AllDirectories). Similar to that, you can use Directory.GetDirectories to get all the folders.
Using those two, and a for each / for each folder in folder, you can simply loop through all the subfolders and get the output exactly as in the sample.
Here is an workflow doing just that, in case you need an example:
example.zip (65.2 KB)
Best Regards,
Bogdan
1 Like