Finding folders on PC with fewer than five files inside


Recommended Posts

This is really messy/inefficient but it gets the job done:

# smallFolderFinder.ps1
# Should be run like:
# smallFolderFinder.ps1 [-path "C:\Path\To\Folder" | -logpath "c:\path\to\log.txt"]
param(
    [string]$path = "C:\",
    [string]$logpath = "c:\smallfolders-$(get-date -format 'yyyyMMddHHmmss').txt"
)

# Create an empty array for our small folders
$smallfolders = new-object system.collections.arraylist

# Look for directories in selected path
write-host "Checking for directories in $path..."
$folders = ls $path -recurse -directory
write-host "Found $($folders.count) directories. "

# Iterator for progress display, look for folders that contain 5 or fewer files
$i = 0
foreach($f in $folders){
    write-progress "Checking $i / $($folders.count)"
    if((ls $f.fullname -r).count -le 5){
        $smallfolders.add($f) | out-null
    }
    $i++
}

# Output results to shell and log
$line = "Found $($smallfolders.count) folders containing 5 or fewer items"
write-host $line
$line | out-file $logpath

foreach($sf in $smallfolders){
    $sf.fullname | out-file $logpath -append
}

It'll pipe out a text document to the log path stating how many folders it found then a list of all the folder paths. If you are scanning a large area (like your whole C drive), this may take a minute to run.

Edited by satukoro
Edited for clarity
  • Like 1
Link to comment
Share on other sites

It's probably obvious to some folks but I realized it may not be immediately obvious to others that the script I posted is a Powershell script. You may have to run the script as an administrator (or an account with the appropriate read/list permissions) in order to view certain directories. Additionally, if you wanted to count the files within folders/subfolders while excluding folders from that count, you could replace this (line 21): 

if((ls $f.fullname -r).count -le 5){

with this:

if((ls $f.fullname -r | where {$_.attribute -notmatch 'directory'}).count -le 5){

I'm pretty sure the only other possible value for "$_.attribute" is "archive" but just to be safe I chose to simply exclude anything that is a directory rather than only include files that are archives.

Edited by satukoro
Link to comment
Share on other sites

$rootPath = "C:\Your\Directory\Path"  # Replace with your starting directory

Get-ChildItem -Path $rootPath -Recurse -Directory | Where-Object {
    $fileCount = (Get-ChildItem -Path $_.FullName -File).Count
    return $fileCount -lt 5
} | ForEach-Object {
    Write-Output $_.FullName
}

This powershell script wil get the job done.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.