#!/usr/bin/perl # ************************************************************************** # # CR_DIR.PRL written by Chris Evans (C.Evans@sghms.ac.uk) # # # program creates three HTM files containing a directory listing of all the # # *.htm files off a certain root directory, and a listing of the non .htm? # # files # # files are ordered by # # directory (search root first then subdirectories in # # alphabetical order) # # time last modified (most recent first) # # size (largest first) # # # # The program takes as input in the variables declared immediately below # # $outputdir -- the directory into which to put these files # # $menu -- filename for the menu to all other reports # # $report -- filename for the directory order output # # $size_rep -- filename for the size sorted output # # $time_rep -- filename for the modification time sorted output # # $other_rep -- filename for the files other than *.htm? # # $startdir -- directory from which to start the recursive search # # $www_offset -- any additional directory reference that accounts for # # a possible difference between the root used by unix for the # # search versus that shown to the world by the HTTP server in use # # This last variable may seem odd but on our local machine the root # # shown to the outside world by the server is at: # # /usr1/www/pages/ # # but my pages (from which I want the search for *.htm files to # # start is at: # # /usr1/www/pages/mhs/psychotherapy/ # # giving the value for $www_offset of /usr1/www/pages # # # file written by Chris Evans (C.Evans@sghms.ac.uk) 28-29.xi.95 # # copyright Chris Evans _BUT_ feel free to distribute this subject to the # # following requirements: # # 1) you do not make a profit on the distribution # # 2) you retain this header information in full # # I would also very much appreciate feedback (I know the programming is # # awful, it's the first piece of PERL I've written and I'm a psychodynamic # # psychotherapist with no intention of giving up my day job so no flames if # # you can resist the temptation) and a copy of enhanced versions if you do # # hack this into something better # # # # Chris Evans (C.Evans@sghms.ac.uk) Section of Psychotherapy, # # St. George's Hospital Medical School, Cranmer Terrace, # # London, SW17 0RE, Britain # # ************************************************************************** # $outputdir = "/usr1/www/pages/mhs/psychotherapy/dir/"; $menu = "menu.htm"; $report = "htm_dir.htm"; $size_rep = "size_dir.htm"; $time_rep = "time_dir.htm"; $other_rep = "oth_file.htm"; $startdir = "/mhs/psychotherapy"; $www_offset = "/usr1/www/pages"; # sort out the location that the server will show for files $http_rep_dir = $outputdir; $http_rep_dir =~ s|$www_offset||o; # o switch as the value of $www_offset is fixed open(FIND, "find . -name \"*.htm\" -print |") || die "Couldn't run find: $!\n"; $tot_size = 0; while ($filename = ) { undef $size; undef $time; chop $filename; # get data on the file ($nsize,$time) = (stat($filename))[7,9]; #use the 7th & 9th items in the stat array # reformat the data ($s, $m, $h, $day, $mon, $yr) = (localtime($time))[0,1,2,3,4,5]; $tot_size += $nsize; $size = &commas9($nsize); $h = &two_digits($h); $m = &two_digits($m); $s = &two_digits($s); $ntime = $h.":".$m.":".$s; $mon = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon]; # reformat the filename to the http server form $href = $filename; $href =~ s|.|$startdir|o; $filedat{$href} = join(';',$nsize, $size, $ntime, $day, $mon, $yr, $time); } open (SUMMARY, ">$outputdir$report") || die "cannot open $outputdir$report"; print SUMMARY "\n\n"; print SUMMARY "Directory structure of $startdir for *.htm files\n"; print SUMMARY "\n\n"; print SUMMARY "

Directory structure of $startdir for *.htm files

\n"; print SUMMARY "

\n";
print SUMMARY "     Size        Last modified       File\n";
print SUMMARY "     ====        =============       ====\n";

$lastdir = ".";
print SUMMARY "$lastdir directory\n";
foreach (sort by_dir keys %filedat) {
   $slash = rindex($_,"/");
   $dir = substr($_,0,$slash);
   $href = $dir;
   $href =~ s/.//;
   $href = $href."/";
   if ($dir ne $lastdir) {
      print SUMMARY "$dir directory\n";
      $lastdir = $dir;
   }
   ($nsize, $size, $ntime, $day, $mon, $yr, $time) = split(/;/,$filedat{$_});
   $nsize{$_} = $nsize;
   $time{$_} = $time;
   $n_htm++;
   printf SUMMARY "%9s  %8s %3s %3s %4s %-s\n", $size, $ntime, $day, $mon, $yr, $_, $_;

}
$kb_size = $tot_size/1000;
$kb_size = &commas13($kb_size);

printf SUMMARY "

\nTotal file size = %13s kb in %5s files
\n", $kb_size, $n_htm; print SUMMARY "\n
\nOutput generated using\n"; print SUMMARY "cr_dir.prl PERL script\n"; print SUMMARY "
\n