I have a list of files and subfolders in a directory, now I will iterate through each and in each iteration If I find subfolder then I will enter that folder and copy the data from the file inside of that subfolder and do some activities.
If I find file then I will just copy data from the file and do some activities.
This I need to do for every file and subfolder present in that sub directory.
I know I that we can use “Directory.GetFiles” for iteration through file or “Directory.GetDirectories” for iteration through folder, but here in my use case it’s actually both.
Any help or suggestion will be helpful.
@Debartha_Mitra_DE - Try this…Bellow query will search through all the subfolders for the all the file types…
Directory.GetFiles(“YourFolderPath”," . ", SearchOption.AllDirectories)
Thanks for your reply, I have run the code and it showed me all the files and the files inside of subfolders.
As you can see below are some of the subfolders present in that directory
I have put some testing file in that subfolder (inside Contracting Vendor IT subfolder) for my development purpose but it’s iterating through this ‘deksopt.ini’ which i don’t want. That file is hidden I think as I can’t see it.
Any suggestion on how not to iterate through this file.
@Debartha_Mitra_DE - Yes you can ignore the .ini as shown below…
Directory.GetFiles(“YourFolderPath”," . “, SearchOption.AllDirectories).Where(Function(n) not path.GetFileName(n).Contains(”.ini")).ToArray
Thanks for providing the useful info, it worked. One more suggestion I need.
As, you can see below my Main folder is “Testproduct 1” where I am doing the iteration.
In the main folder, as you can see there are some subfolders which contain some files. I need to do a different set of activities on the files which are under this subfolders so for that I need to separate my if condition and identify these files which are under subfolders.
So is there anyway I can identify which files are under subfolders and which are not.
Please let me know.
Thanks in advance.
@Debartha_Mitra_DE - When you Iterate through For Each= File → FilesList(Say)
Inside the for each, use write Line = path.GetDirectoryName(File), which gives you the entire path excluding the file name…so you can use switch condition or If Condition by using .Contains function to find out what folder it is reading and do you actions…Please see the sample below…
I have 2 sub folders(Test1 and Test2) unders my Main Folder called “Files”
Thanks for your useful input.
The example you have just provided is really helpful.
Is there anyway I can get subfolder name only such as, ‘Test1’ or ‘Test2’ not the full path or can I get only the name of the main folder (for your case ‘Files’)
In my case, the scenario is if one file is under one subfolder then i need to treat in a particular way and if it’s under main folder then i need to treat it in another way.
@Debartha_Mitra_DE - Please try this…
path.GetDirectoryName(File) → This will first just directory Name of that file…
Path.GetFileNameWithoutExtension → Even though you think GetFileNameWithoutExtension will get you only filenames without extension, but when you pass the entire folderpath to this command will print just last Part…which is Test1 and Test2…
Hope this helps…
Thanks a lot, Actually I was going through this particular code as well but at first I thought it will give file name without extension that’s why I avoided it.
Many many thanks.
It will be helpful if you let me know where can I get linq query information that you mentioned in “Directory.GetFiles(“YourFolderPath”,” . “, SearchOption.AllDirectories).Where(Function(n) not path.GetFileName(n).Contains(”.ini")).ToArray"
I would love to go through it and use it for my future reference.
@Debartha_Mitra_DE - When I was working on a project, I need to ignore the file starting with ~$. so started searching and found some of the useful codes(to be honest)
lot of items, you will find these codes in stackoverflow or in UiPathForum…those are my key source…plus microsoft.docs
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.