For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2007 > Listing files and their sizes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Listing files and their sizes
Telemachus Odysseos

2007-09-18, 7:02 pm

Hi,

I have read around and cannot seem to find an answer to this, and it's
driving me a bit nuts. I have a small script that I am trying to use to list
the items in a directory and the size of the items. The script works
perfectly if I run it in the directory itself, but if the script is
somewhere else in my system, it prints the filenames but not the sizes. I
have tried changing ownership of the perl script every which way (thinking
it's a permissions problem), but even if the script is owned by root, no
joy. Here is the script. Any ideas would be greatly appreciated:

#!/usr/bin/perl

opendir(CD,"/path/to/directory/here");
while ( $_ = readdir(CD)) {
next if $_ eq "." or $_ eq "..";
print $_, " " x (30-length($_));
print (-s $_ );
print "\n";
}

Dr.Ruud

2007-09-18, 7:02 pm

"Telemachus Odysseos" schreef:

> [...] it prints the filenames
> but not the sizes. [...]


perldoc -f readdir:
"you'd better prepend the directory in question".


> #!/usr/bin/perl


Missing:

use strict;
use warnings;

> opendir(CD,"/path/to/directory/here");


my $dir_name = "/path/to/directory/here";
opendir my $dir_handle, $dir_name or die "$!";

> while ( $_ = readdir(CD)) {


while (my $dir_entry = readdir $dir_handle) {


> next if $_ eq "." or $_ eq "..";


next if -d "$dir_name/$dir_entry";

> print $_, " " x (30-length($_));
> print (-s $_ );
> print "\n";


printf "%30s %d\n", $dir_entry, -s _;

> }



--
Affijn, Ruud

"Gewoon is een tijger."
davidfilmer@gmail.com

2007-09-18, 7:02 pm

On Sep 18, 1:59 pm, telemachu...@gmail.com (Telemachus Odysseos)
wrote:

> The script works
> perfectly if I run it in the directory itself, but if the script is
> somewhere else in my system, it prints the filenames but not the sizes.


That's because readdir returns a plain filename. You need to either
chdir() to the directory within your script, or fully qualify the
filename (ie, -s /path/to/my.file)


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)


John W. Krahn

2007-09-18, 7:02 pm

Telemachus Odysseos wrote:
> Hi,


Hello,

> I have read around and cannot seem to find an answer to this, and it's
> driving me a bit nuts. I have a small script that I am trying to use to list
> the items in a directory and the size of the items. The script works
> perfectly if I run it in the directory itself, but if the script is
> somewhere else in my system, it prints the filenames but not the sizes. I
> have tried changing ownership of the perl script every which way (thinking
> it's a permissions problem), but even if the script is owned by root, no
> joy. Here is the script. Any ideas would be greatly appreciated:
>
> #!/usr/bin/perl
>
> opendir(CD,"/path/to/directory/here");


You should *ALWAYS* verify that the directory opened correctly:

opendir CD, '/path/to/directory/here'
or die "Cannot open '/path/to/directory/here' $!";


> while ( $_ = readdir(CD)) {
> next if $_ eq "." or $_ eq "..";
> print $_, " " x (30-length($_));
> print (-s $_ );
> print "\n";
> }


As it states in the documentation for readdir():

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;


So you need code something like:

#!/usr/bin/perl
use warnings;
use strict;

my $dir = '/path/to/directory/here';

opendir CD, $dir or die "Cannot open '$dir' $!";
while ( my $entry = readdir CD ) {
next if $entry eq '.' or $entry eq '..';
printf "%-30s%d\n", $entry, -s "$dir/$entry";
}
closedir CD;




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
useperl-jeff@yahoo.com.cn

2007-09-18, 10:01 pm


--- Telemachus Odysseos <telemachus07@gmail.com> wrote:

>
> #!/usr/bin/perl
>
> opendir(CD,"/path/to/directory/here");
> while ( $_ = readdir(CD)) {
> next if $_ eq "." or $_ eq "..";
> print $_, " " x (30-length($_));
> print (-s $_ );


Here you need to specify the absolute path for the files.
print -s "/path/to/dir/$_";

> print "\n";
> }
>




________________________________________
________________________________________
____
Sick of deleting your inbox? Yahoo!7 Mail has free unlimited storage.
http://au.docs.yahoo.com/mail/unlimitedstorage.html

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com