This tool builds a tree of directorys and files and for each entry in the tree shows stats for the entry.

For subdirectorys the number of nested directorys (if any) are shown with the file count and the total size of the files within the directory are shown.

For files the file size is shown.

It would be trivial to show other information for each node.

use warnings; use strict; use Tk; use Tk::Tree; my $rootPath = shift; ShowHelp () if ! defined $rootPath; ShowHelp (-2, "Error finding folder $rootPath\n\n") if ! -d $rootPath; my $main = MainWindow->new (-title => "Folder stats for $rootPath"); my $tree = $main->ScrlTree ( -font => 'FixedSys 8', -itemtype => 'text', -separator => '/', -scrollbars => "osoe" ); my @pathStack = (1); my $maxNesting = 0; my $totalLines; my $maxLineLength = 0; my $currPath = join "/", @pathStack; $tree->add ($currPath, -text => $rootPath); my ($subDirCount, $subFileCount, $subTotalSize) = buildSubTree ($tree, $rootPath, \@pathStack, \$maxNesting, \$totalLines, \$maxLin +eLength); my $annotation = $rootPath . " \t("; my $plural = $subDirCount != 1 ? 's' : ''; $annotation .= $subDirCount . " dir$plural, " if $subDirCount; $annotation .= "$subFileCount files, $subTotalSize bytes)"; $tree->entryconfigure ($currPath, -text => $annotation); $totalLines = 40 if $totalLines > 40; $main->geometry (($maxLineLength + @pathStack * 4) * 5 . 'x' . (40 + $ +totalLines * 20)); closeTree ($tree, ''); $tree->pack(-fill=>'both',-expand => 1); MainLoop; sub closeTree { my $tree = shift; my ($entryPath, $hideChildren) = @_; my @children = $tree->info (children => $entryPath); return if ! @children; for (@children) { closeTree ($tree, $_, 1); $tree->hide ('entry' => $_) if $hideChildren; } $tree->setmode ($entryPath, 'open') if length $entryPath; } sub buildSubTree { my ($tree, $rootPath, $pathStack, $maxNesting, $totalLines, $maxLine +Length) = @_; my $dirCount = 0; my $fileCount = 0; my $sizeTotal = 0; push @$pathStack, 1; $$maxNesting = @$pathStack if $$maxNesting < @$pathStack; my $dir; opendir $dir, $rootPath; while (my $currDir = readdir $dir) { next if $currDir =~ /^\.\.?$/; my $path = "$rootPath/$currDir"; ++$$totalLines; my $currPath = join "/", @$pathStack; my $nodeText = $path; $tree->add ($currPath, -text => $nodeText); if (-d $path) { my ($subDirCount, $subFileCount, $subTotalSize) = buildSubTree ($tree, $path, $pathStack, $maxNesting, $totalLines, $maxLineL +ength); $dirCount += $subDirCount + 1; $fileCount += $subFileCount; $sizeTotal += $subTotalSize; my $annotation = $nodeText . " \t("; my $plural = $subDirCount != 1 ? 's' : ''; $annotation .= $subDirCount . " dir$plural, " if $subDirCount; $annotation .= "$subFileCount files, $subTotalSize bytes)"; $tree->entryconfigure ($currPath, -text => $annotation); } else { my $fileSize = -s $path; $tree->entryconfigure ($currPath, -text => $nodeText . " ($fileS +ize bytes)"); ++$fileCount; $sizeTotal += $fileSize; } $$maxLineLength = length ($nodeText) if length ($nodeText) > $$max +LineLength; ++$pathStack->[-1]; } closedir $dir; pop @$pathStack; return ($dirCount, $fileCount, $sizeTotal); } sub ShowHelp { my $exitValue = 0; $exitValue = shift if defined $_[0] and $_[0] =~ /^[-+]?\d+$/; print $_ while $_ = shift; print <<HELP; FolderStats scans a directory tree starting at the folder given on the + command line and generates an explorer like tree giving folder content stats s +uch as number of files, their total size, and the count and sizes of sub-fold +ers. Note that the statistics are not dynamically updated as files and fold +ers are altered on disk. Usage: FolderStats <root folder> HELP exit ($exitValue || -1); }

DWIM is Perl's answer to Gödel

In reply to Directory tree explorer with stats reporting by GrandFather

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.