Get the Files count for each SubFolder Separately

Hi Guys,

I tried to get the File count of each Sub Folder Separately. For example, Attached is the Folder Structure.


Inside each region Folder, I have some documents attached is the screenshot

Results:The outcome of the Files Count Should be
America -2
Australia -1
Please guide me to the Solution for this task!
Thanks,
Happy Automation

Follow this steps

1.Use the “Assign” Activity to Get Folder Paths: You can use the Assign activity to assign an array of folder paths. For example, you can assign an array with the paths to the “America” and “Australia” folders.

2.Iterate Through the Folder Paths: Use a For Each loop to iterate through the array of folder paths.

3.Use the “Directory.GetFiles” Method: Within the loop, use the Directory.GetFiles method to get a list of files in each subfolder.

4.Use the “Directory.GetFiles” Method: Within the loop, use the Directory.GetFiles method to get a list of files in each subfolder.
5.Count the Files: Use the .Count property on the list of files to determine the file count in each subfolder.

@Iswarya_P1

Hi @Iswarya_P1

Assign rootFolderPath = "C:\Your\Root\Folder\Path"
Assign subfolders = Directory.GetDirectories(rootFolderPath)

For Each subfolder In subfolders
    Assign currentSubfolder = subfolder
    Assign folderName = Path.GetFileName(currentSubfolder)  // Extract just the folder name
    Assign fileCount = Directory.GetFiles(currentSubfolder).Count
    Assign folderCountString = folderName + " - " + fileCount.ToString()
    Log Message: folderCountString  // Log the result
End For Each

Hope it helps!!

Assign activity: folderPaths = {“C:\Path\To\America”, “C:\Path\To\Australia”}

For Each activity (item in folderPaths):
Assign activity: files = Directory.GetFiles(item)
Assign activity: folderName = Path.GetFileName(item)
Assign activity: fileCount = files.Count

// Now you can log or store the folder name and file count
Log Message activity: "Folder " + folderName + " - " + fileCount

// You can also store the results in a DataTable or a dictionary for further processing

@Iswarya_P1

Hi @Iswarya_P1 ,
You can try
Directory.GetFiles(Parent Folder,“*”, SearchOption.AllDirectories).Count

regards,

Hey @Iswarya_P1 ,

I have recreated your problem scenario, and below is the output I obtained

Below is the Linq

(From subfolder In Directory.GetDirectories("Input\") Let files = Directory.GetFiles(subfolder, "*", SearchOption.AllDirectories) Select subfolder.Split("\").Last & " = " & files.Length.ToString).ToArray()

below is my folder structure
image

And below is the file
File_Count_Forum.zip (52.0 KB)

Hope it helps you!

Hi

Well there are two possible scenarios here
So choose from below expression whichever suits your need

Try with expression in a assign activity if you have a folder and subfolders in it with files in it

Stroutput = String.Join(Environment.NewLine, Directory.GetDirectories(parentFolderPath).
    Select(Function(subfolder) Path.GetFileName(subfolder)+”_”+Directory.GetFiles(subfolder).Count()).
    ToList())

This gives exactly like this

Try with expression if u have a folder an subfolder with files and further subfolders as well

stroutput = String.Join(Environment.NewLine, Directory.GetDirectories(parentFolderPath, "*", SearchOption.AllDirectories).
    Select(Function(subfolder) Path.GetFileName(subfolder)+”_”Directory.GetFiles(subfolder).Count).
    ToList())

Cheers @Iswarya_P1

@Iswarya_P1

This gives a dictionary…with key as the sub folder name and the value as count as you need

Directory.GetDirectories(rootFolder).ToDictionary(Function(subFolder) New DirectoryInfo(subFolder).Name, Function(subFolder) Directory.GetFiles(subFolder).Count)

Rootfolder is where you need to give the root folder value

This gives a dictionary like

Australia - 1
Anerica - 2

Cheers

2 Likes