#!/usr/bin/perl use File::Find; use strict; use Time::localtime; use File::Spec; my $cur = File::Spec->curdir; my $up = File::Spec->updir; my $dirtot; my ($dir, @parts, $error, $starttime, $runtime, $endtime, $runmin, $year, $month, $day, $tm, $date, $rootdir, $userdir, $slash, $count, $runhour, $starttime, $runtime, $endtime, $runmin, $year, $month, $day, $tm, $date); $starttime = (time); $slash = "//"; #Opens the input and output files open(IN, "< input.txt") or die("Couldn't open input.txt\n"); open(OUT, "> output.csv") or die("Couldn't open output.csv\n"); #Separates the newline delimited rows chomp ( @parts = ); #Used to calculate how long the program took to run $starttime = (time); #Prints headings print OUT "Path,User,MB,Date,Error\n"; #Displays total for subdirectories foreach my $start (@parts) { my @dirs = &find_subdirs($start); foreach my $dir (@dirs) { $tm = localtime; $year = $tm->year+1900; $month = $tm->mon+1; $day = $tm->mday; $date = "$month-$day-$year"; ($rootdir,$userdir) = split /$slash/,$dir; print "\tWalking $dir\n"; my $total = 0; find sub { $total += -s}, $dir; $dirtot = $dirtot + $total; #Used for determining total directory size $total = ($total / 1024) / 1024; #Converts to MB $total = sprintf("%0.2f", $total); #Formats output to 2 decimals print OUT"$rootdir,$userdir,$total,$date,$!\n"; } $dirtot = ($dirtot / 1024) / 1024; #Converts to MB $dirtot = sprintf("%0.2f", $dirtot); #Formats total to 2 decimals print STDERR "$start: No subdirectories\n" unless @dirs; } $endtime = (time); $runtime = $endtime - $starttime; $runmin = $runtime/60; $runmin = sprintf("%0.1f", $runmin); $runhour = $runmin/60; $runhour = sprintf("%0.1f", $runhour); if ($runtime <= 60){print "\n\nCompleted processing in $runtime seconds\n\n";} elsif ($runmin <=60) {print "\nCompleted processing in ". $runmin . " minutes\n\n";} else {print "\nCompleted processing in ". $runhour ." hours\n\n";} # This closes output file and then calls an external executable that # will insert CSV output into an MS Access database table close(OUT); system "csv2access.exe"; print "\nOutput created."; sub find_subdirs { my $start = shift; unless(opendir(D, $start)) { warn "$start: $!\n"; next; } my @dirs = map { -d "$start/$_" && !-l "$start/$_" && $_ ne $cur && $_ ne $up ? "$start//$_" : () } readdir(D); closedir(D); @dirs; }