2

I am trying to count all files in subfolder and extract to csv. Here is the file structure.

C:\TEST\FolderAAA

  |-folderA1
     |-folderA1.1
        |-file
        |-file
     |-folderA1.2
        |-file
        |-file
     |-folderA1.3
        |-file
        |-file

   |-folderA2
     |-folderA1.1
        |-file
        |-file
     |-folderA2.2
        |-file
        |-file
     |-folderA2.3
        |-file
        |-file

C:\TEST\FolderBBB

  |-folderB1
     |-folderB1.1
        |-file
        |-file
     |-folderB1.2
        |-file
        |-file
     |-folderB1.3
        |-file
        |-file

   |-folderB2
     |-folderB2.1
        |-file
        |-file
     |-folderB2.2
        |-file
        |-file
     |-folderB3.3
        |-file
        |-file

C:\TEST\FolderCCC

  |-folderC1
     |-folderC1.1
        |-file
        |-file
     |-folderC1.2
        |-file
        |-file
     |-folderC1.3
        |-file
        |-file

   |-folderC2
     |-folderC2.1
        |-file
        |-file
     |-folderC2.2
        |-file
        |-file
     |-folderC3.3
        |-file
        |-file

I am looking for a CSV output with the below format:

Column A          Column B
Folders           File Count
----------------------------
FolderAAA                   0
folderA1                    0
folderA1.1                  2
folderA1.2                  2
folderA1.3                  2
folderA2                    0
folderA2.1                  2
folderA2.2                  2
folderA2.3                  2

Space/New Blank Row --------

FolderBBB                   0
folderB1                    0
folderB1.1                  2
folderB1.2                  2
folderB1.3                  2
folderB2                    0
folderB2.1                  2
folderB2.2                  2
folderB2.3                  2

Space/New Blank Row --------

FolderCCC                   0
folderC1                    0
folderC1.1                  2
folderC1.2                  2
folderC1.3                  2
folderC2                    0
folderC2.1                  2
folderC2.2                  2
folderC2.3                  2

I tried and modified this code that I found after doing some research here:

$FOLDER_ROOT = "C:\TEST\"
$OUTPUT_LOCATION = "C:\TEST\RESULT\Folder_Count.txt"
$OUTPUT_CSV = "C:\TEST\RESULT\Folder_Count.csv"

function DirX($directory) {
    Remove-Item $OUTPUT_LOCATION

    foreach ($singleDirectory in (Get-ChildItem $directory -Recurse -Directory)) {
        $count = Get-ChildItem $singleDirectory.FullName -File |
                 Measure-Object |
                 %{$_.Count}
        $summary = $singleDirectory.Basename + " - " + $count
        Add-Content $OUTPUT_LOCATION $summary
    }
}
DirX($FOLDER_ROOT)

Import-Csv $OUTPUT_LOCATION -Delimiter "-" -Header Folder, Count |
    Export-Csv $OUTPUT_CSV -NoTypeInformation

The extract I am getting does not format according to what I am trying to achieve.

Currently the extract is formatted incorrectly this way:

Column A          Column B
Folders           File Count
----------------------------
FolderAAA                  0
folderA1                   0
folderA2                   0
folderA1.1                 2
folderA1.2                 2
folderA1.3                 2
folderA2.1                 2
folderA2.2                 2
folderA2.3                 2
4
  • 1
    It appears that using Get-ChildItem -Recurse -Directory traverses the directory structure using breadth first search. This is the root cause of why the output shows up in the order AAA -> A1, A2 -> etc.. One solution is to perform the traversal yourself by implementing a depth first traversal algorithm. Commented Aug 28, 2019 at 1:55
  • Thanks Josh, I am afraid this is too technical for me. I am still a beginner with Powershell. Would there be a guide or reference for me to do some research somewhere that you could recommend? Thanks again. Commented Aug 28, 2019 at 5:50
  • Traversals are a fun topic that typically gets covered in the first year of a computer science curriculum at highschool or university. It's a beginner friendly topic when covered on its own; unfortunately, most of the material online when you search "depth first search" assumes you're working with binary trees, and uses the language of graphs, edges, leafs, and nodes, instead of speaking directly about directories and files as you're working with in your problem. You might find some luck exploring the topic by searching, "depth first" + "traversal" + "directories" -graph on google? Commented Aug 28, 2019 at 6:44
  • Thanks Josh for the guidance! Commented Aug 28, 2019 at 22:45

2 Answers 2

2

I concur with @Scepticalist's suggested approach, but would recommend streamlining it into a pipeline, like this:

Get-ChildItem $FOLDER_ROOT -Recurse -Directory |
    Sort-Object FullName |
    Select-Object FullName, @{n='Count';e={(Get-ChildItem $_.FullName -File).Count}} |
    Export-Csv -Path $OUTPUT_CSV -NoType
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ansgar - I made the updates based on your suggestions to @Scepticalist approach
1

Why not gather the data first, then sort it and output?

$FOLDER_ROOT = "C:\TEST"
$OUTPUT_CSV = "C:\TEST\Folder_Count.csv"

Function DirX($directory) {
    Write-Host "Working..." -NoNewline
    $Data = Get-ChildItem $directory -Recurse -Directory | ForEach-Object {
        Write-Host "."-NoNewline
        $count = (Get-ChildItem $_.FullName -File).count #  | Measure-Object | %{$_.Count}
        [pscustomobject]@{FilePath = $_.fullName;Filecount = $count}
    }
    Write-Host "Done"
    $Data | Sort-Object Filepath
}


$ThisPath = Dirx $FOLDER_ROOT
$ThisPath | Export-Csv -Path $OUTPUT_CSV -NoTypeInformation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.