Hack 83 Show a Directory Structure as a Word OutlineUse Outline view to quickly scan a directory for errant files or space hogs with this RTF hack. When directory structures were small, you could figure out where all the space on your hard drive went by just using a bit of DOS at the command line: > dir /os/n c:\somedir > summary.txt But these days, a typical hard drive just has too many incidental subdirectories, and finding the large files (or a directory full of a million small files) in that summary.txt file would be like finding a needle in a haystack.
But by massaging the data a bit and turning it into a Word outline, you can use the collapsible levels feature in Outline view to quickly sift through the data and find the unexpected space hogs. 9.5.1 The Code
This small Perl program surveys a directory that you specify and saves it to an RTF file, using a filename that you specify. Save this script as directoryoutline.pl. use strict; use File::Find; my @items; my $min_depth = 999; my($dir, $out) = @ARGV; die "Usage:\n $0 drive:/dir/to/scan output.rtf" unless @ARGV == 2 and -d $dir and $out =~ m/\.rtf$/is; Scan_dirs( $dir ); open R, ">", $out or die "Can't write-open $out: $!"; RTF_tree( ); close R; print "Surveyed $dir to $out\n"; exit; sub Scan_dirs { my $count; my %dirsize; finddepth( { follow => 0, wanted => sub { if( -f $File::Find::name ) { $dirsize{ $File::Find::dir } += -s _; } elsif( -d _ ) { $dirsize{ $File::Find::dir } += $dirsize{ $File::Find::name }; my $depth = $File::Find::name =~ tr{/\\}{}; $min_depth = $depth if $depth < $min_depth; unshift @items, [ $depth, $dirsize{ $File::Find::name }, ($_ eq '.') ? $File::Find::name : $_ ]; } } }, $_[0] ); return; } sub RTF_tree { die "Nothing to report?!" unless @items; print R q[{\rtf1\ansi\deff0 {\fonttbl {\f0 \froman Times New Roman;}} \viewkind2 \fs24 ]; foreach my $item ( @items ) { my( $depth, $size, $name ) = @$item; $depth -= $min_depth; next if $depth > 8; printf R "\\outlinelevel%s {\\i %s \\scaps kb} : %s\\par\n", $depth, with_commas( int(.5 + $size/1024) ), rtf_escape_broadly( $name ); } print R "}\n"; return; } sub with_commas { my $x = $_[0]; 1 while $x =~ s/^(\d+)(\d{3})/$1,$2/; return $x; } sub rtf_escape_broadly { my $s = $_[0]; $s =~ s/(\W)/"\\'".(unpack("H2",$1))/eg; return $s; } 9.5.2 Running the Hack
To see the structure of the Perl directory on your system, open up a DOS prompt and navigate to your top-level C:directory. Enter the following at the DOS prompt: > perl directoryoutline.pl "C:\Perl" "C:\PerlDirOutline.rtf" Figure 9-7 shows the output PerlDirOutline.rtf file in Word's Outline view. You can collapse and expand your view of each folder, as with any Word outline. Figure 9-7. The Perl directory, shown in Word's Outline viewThis view shows that roughly a quarter of the disk space used by the
Perl directory is actually just (expendable) HTML versions of the
standard Perl documentation ( Sean M. Burke |