Urgent help needed

I have some problem reading dirs. I'm using widnwows xp on NTFS, and i have problem when reading directory different from those in which one i'm running the script...

$dir = 'D:\My Documents\My Music\New2';
opendir (DH, $dir);
while ($_ = readdir(DH)) {
next if -d $_;
print $_;
print " f " if -f $_;
print -s if -f $_ and -r $_;
}
closedir(DH);

when running this one it return only the name of the files, no size, or any other stat. Running this for the current dir: $dir = '.'; works just fine.

I tried to add the path into $ENV{PATH} but the result is the same. So can anyone explain me how to read other dirs using full or relative path?
manniakkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kblack05Commented:
my $dir = 'D:\My Documents\My Music\New2';
opendir (DH, $dir) || die "Can't read from the source tree: $!\nPerhaps the path or permissions are wrong...\n";
while ($_ = readdir(DH)) {
next if (-d $_);
print $_,"\n";
print " f " if (-f $_);
my $size = (-s $_);
print $size if (-f $_ && ( -r $_));
}
closedir(DH) || die "Can't close the dir: $!\n";


Regards,

~K Black
0
kblack05Commented:
Sorry that's

print "$size " if (-f $_ && ( -r $_));

There was no space between elements in the pout in my
last post so the output was a bit bunched up...

~KB
0
jmcgOwnerCommented:
In order to use the file test operators on filenames returned by readdir, you must prepend the filename with the name of the directory (except in the case of the current directory).

my $dir = 'D:\My Documents\My Music\New2';
opendir (DH, $dir) || die "Can't read from the source tree: $!\nPerhaps the path or permissions are wrong...\n";
while ($_ = readdir(DH)) {
my $f = $dir . '\' . $_;
next if (-d $f);
print $f,"\n";
print " f " if (-f $f);
my $size = (-s $f);
print $size if (-f $f && ( -r $f));
}
closedir(DH) || die "Can't close the dir: $!\n";

You may also see people choosing to do a chdir to the directory to avoid doing this.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

kblack05Commented:
Sorry jmcq, that must be a windows thing. I was using it
from Unix.

I will remember that for ActivePerl and such, thanks!!

~K Black
0
jmcgOwnerCommented:
Ooops, indeed. I see I picked up Kblack's code instead of Manniakk's.

The diagnosis remains the same, but working out the formatting details will have to wait until after dinner.
0
TintinCommented:
Use the docs Luke.

perldoc -f readdir

              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;
0
bcladdCommented:
You can also use the packages available with Perl:

use File::Find;
$dir = 'C:\Hold';

sub file_filter {
    return if $_ eq '.' or $_ eq '..';
    if (-d $_) {
     $File::Find::prune = 1;
     return;
    }
    print $File::Find::name;
    print " f " if -f $_;
    print -s if -f $_ and -r $_;
    print "\n";
}

find \&file_filter, $dir;

This works on WinXP in the current or another directory. prune is set so subdirectories are NOT explored.
0
jmcgOwnerCommented:
Here's a version that follows quite closely your original script. I took advantage of the default arg $_ and the special pseudo-handle "_" to make things look simpler and to avoid extra system calls.

$dir = 'D:\My Documents\My Music\New2';
opendir (DH, $dir) or die "Could not opendir $dir";

foreach ( readdir(DH) ) {
  $_ = $dir . '\' . $_;
  next if -d; # also eliminates . and ..

  print $_;
  if ( -f _) {
    print " f ";
    print -s _ if defined -s _;
    }
  print "\n";
}
closedir(DH);

What was the point of the restriction to only print the size on files where -r was true? I changed it to print the size whenever the size was available.

0
manniakkAuthor Commented:
i think that you deserve this points, because that you was the first, i will optimize the code so, the avoid of system calls is not vital.\ for me at this point...
0
TintinCommented:
Here's an even easier way (IMHO)

foreach (<D:/My Documents/My Music/New2/*>) {
  next if -d;
  print $_;
 
  if (-f) {
    print " f ";
    print -s if defined -s;
  }
 
  print "\n";
}

0
jmcgOwnerCommented:
I like that, Tintin. In my experience, getting the right thing to happen with glob operators on DOS/Windows is iffy, but what you've done here is very nice and clean.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.