Bret Goodfellow wrote:
I am writing a script to list out a directory's contents, showing the
number of days since modified.  The problem I am having is that the
script doesn't list out the "modified time" unless I change to the
directory being listed.  If I change to the directory I want to list,
then all works okay.  Is there a way to fix this script so that I don't
have to run the script from the current directory?

Once again (sigh):

perldoc -f readdir
    readdir DIRHANDLE
            Returns the next directory entry for a directory opened by
            "opendir".  If used in list context, returns all the rest of the
            entries in the directory.  If there are no more entries, returns
            an undefined value in scalar context or a null list in list
            context.

            If you're planning to filetest the return values out of a
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            "readdir", you'd better prepend the directory in question.
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            Otherwise, because we didn't "chdir" there, it would have been
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            testing the wrong file.
            ^^^^^^^^^^^^^^^^^^^^^^

                opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
                @dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR);
                closedir DIR;


use strict;
use warnings;
use File::find;
use File::stat;
my $arg_length;
my $arg_lastchar;
my $arg_string;
my $Len;
$arg_length = length($ARGV[0]);
$arg_lastchar = substr($ARGV[0], $arg_length-1, 1);

Or simply:

my $arg_lastchar = substr $ARGV[0], -1;


$arg_string = $ARGV[0];
print "Argument: $arg_string\n";
print "length: $arg_length\n";
print "last character: $arg_lastchar\n";
print "Contents of $arg_string\n";
opendir DH, $arg_string or die "Couldn't open directory: $arg_string
$!";
########################################################################
#
# Read one file at a time into $_
#
########################################################################
#
while ($_ = readdir(DH)) {
next if $_ eq "." or $_ eq "..";
next if -d $_ ;

next if -d "$arg_string/$_";


#####################################
# append upto 30 blanks after the file name #
#####################################
print $_, " " x (30-length($_));

printf '%-30s', $_;


print " age of file: "; # age of file
$Len = index(-M $_, ".");


print substr(-M $_, 1, $Len-1);
                       ^
You do realize that strings start at 0 and not 1?


print int( -M _ );


print "\n";

Or combine all the print statements into one:

   printf "%-30s age of file: %d\n", $_, int( -M _ );


}



John -- use Perl; program fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to