List SubFolders and Save them to a File

Dears,

I want to List SubFolders in one Folder and Save the extracted list to a File ( TXT, CSV,…etc).Thx

Hi
hope this expression would help you resolve this
–to get the sub folders in a folder
arr_folderpath = Directory.GetFiles(“yourfolderpath”,"**.*",SearchOption.AllDirectories)
where arr_folderpath is a variable of type string array
–now use a WRITE TEXT FILE activity where mention this as input string
String.Join(Environment.Newline,arr_Folderpath)
and mention the file path of the text file

Cheers @hsendel

1 Like

is it working
kindly let know for any queries or clarification
@hsendel

1 Like

Thanks @Palaniyappan, It’s working but the output is the complete path of subfolders, I need only subfolders names. Thx

Then use Directory.GetDirectories to retrieve all the sub folders inside

1 Like

YES I already fix this using : GetDirectories instead of GetFiles
but the output is the complete path of subfolders, I need only subfolders names. Thx

I will try this : System.IO.Path.GetDirectories(item.tostring).split("."c)(0) using for each

You have to do string manipulations then to get the names like

within the for loop, item.Split("\"c).Last which will give you the name

1 Like

I use this : System.IO.Path.GetFileName(item.tostring).Split(""c).ToString, not working

yeah, if you want to use GetFileName, then you don’t need split anymore, it will give you the name of the folder @hsendel

1 Like

Hi,

In a Write text file activity this will write all the directory names alone,

string.Join(Environment.NewLine, Directory.GetDirectories(“yourDirPath”).[Select](Function(d) New DirectoryInfo(d).Name).ToList())

2 Likes

@hsendel,

if this resolved, please mark it as solved from your end.

1 Like

Thanks @sarathi125, Well done :slight_smile:

Last Question @sarathi125, Can we use this list in text file as input for “input dialogue” ?

Just as an added note, if you only need the names this method is faster and more memory efficient: System.IO.Directory.EnumerateDirectories(“C:\StartFolder”, “*”, System.IO.SearchOption.AllDirectories) and you can use @sarathi125 way to clean the fullpaths

Thanks @bcorrea, Both are solutions but I opt for @sarathi125 Solution as the problem fixed with only one command and without even declaring variables :slight_smile:

1 Like

yes, i dont mean to change the whole thing, just the method from GetDirectories to EnumerateDirectories, but it is your choice if you want to use a slower method :wink:

1 Like

Yes, you can pass this query in the “Label” property of the Input Dialogue activity, you can see the folder names in the input dialogue.

not in label need to be pasted in “array”, I did it but it’s not working
Hers’ the error message :
Compiler error(s) encountered processing expression “string.Join(Environment.NewLine, Directory.GetDirectories(“C:\UiPath”).[Select](Function(d) New DirectoryInfo(d).Name).ToList())”.
Value of type ‘String’ cannot be converted to ‘1-dimensional array of String’.

Any fix for that ? or need to open another topic?