#!/usr/bin/perl # ************************************************************************** # # filesize.prl written by Chris Evans (C.Evans@sghms.ac.uk) # # This program finds all your HTML files (if you give it some pointers, see # # below!) and places a note of the size of the locals files pointed to at # # end of the HREF. Currently it only handles local calls with absolute # # filenames, i.e. it won't work on calls like tmp.lst"); # sometimes used for debugging! # @filemask = ("intro.htm"); # again, used for debugging only! # the program plays very safe and makes backup copies of all the files # it changes by appending this backup extension to the filename # NOTE: intro.htm becomes intro.htm.bak NOT intro.bak # I didn't have time to change this to the more sensible form $bak = ".bak"; # backup extension # **** That's the end of user input I hope **************************** &getdata; # now run through all subdirectories looking for files foreach $mask (@filemask) { open(FIND, "find . -name \"$mask\" -print |") || die "Couldn't run find: $!\n"; while ($filename = ) { chop($filename); $touched{$filename} = 1; print "\nProcessing file $filename...\n"; $backname = $filename . $bak; rename($filename, $backname) || die "Couldn't rename $filename as $backname\n"; open (NEW, ">$filename") || die "Couldn't open new file as $filename\n"; open (OLD, "$backname") || die "Couldn't open $filename\n"; while () { if (//) { # test to see if the link has already been given a size s/({[^<]+)/{$size}/; # if so, replace that } else { s/<\/A>/ {$size}<\/A>/; # if not, create a size label } } # end of if (/) { undef $size; chop $filename; # get data on the file ($nsize,$time) = (stat($filename))[7,9]; #use the 7th & 9th items in the stat array # reformat the size if (-d _) { $nsize = "dir"; } elsif ($nsize <= 1000) { $size = "<1k"; } elsif ($nsize < 1000000) { $size = int($nsize/1000); $size = $size."kb"; } else { $size = int($nsize/1000000); $size = $size."Mb"; } # reformat the date and time data ($s, $m, $h, $day, $mon, $yr) = (localtime($time))[0,1,2,3,4,5]; $tot_size += $nsize; $h = &two_digits($h); $m = &two_digits($m); $s = &two_digits($s); $day = &two_digits($day); $ntime = $h.":".$m.":".$s; $montxt = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon]; $mon++; $mon = &two_digits($mon); $touch = $mon.$day.$h.$m.$yr; # reformat the filename to the http server form $href = $filename; $href =~ s|.|$startdir|o; # put this into two associative arrays for future use $size{$href} = $size; $date{$filename} = $touch; } close TIME; } # &getdata sub two_digits { local ($_) = @_; if (length($_) == 0) { $_ = "00"; } elsif (length($_) == 1) { $_ = "0".$_; } elsif (length($_) > 2) { die "Supplied a number of more than 2 digits to two_digits\n"; } $_; }