?
Solved

Urgent help needed

Posted on 2003-03-17
11
Medium Priority
?
200 Views
Last Modified: 2010-03-05
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?
0
Comment
Question by:manniakk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 11

Expert Comment

by:kblack05
ID: 8155383
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
 
LVL 11

Expert Comment

by:kblack05
ID: 8155408
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
 
LVL 20

Accepted Solution

by:
jmcg earned 500 total points
ID: 8155505
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:kblack05
ID: 8155568
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
 
LVL 20

Expert Comment

by:jmcg
ID: 8155623
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
 
LVL 48

Expert Comment

by:Tintin
ID: 8155839
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
 
LVL 11

Expert Comment

by:bcladd
ID: 8156337
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
 
LVL 20

Expert Comment

by:jmcg
ID: 8156646
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
 

Author Comment

by:manniakk
ID: 8161308
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
 
LVL 48

Expert Comment

by:Tintin
ID: 8162979
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
 
LVL 20

Expert Comment

by:jmcg
ID: 8163545
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question