manniakk
asked on
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?
$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?
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
from Unix.
I will remember that for ActivePerl and such, thanks!!
~K Black
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.
The diagnosis remains the same, but working out the formatting details will have to wait until after dinner.
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;
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;
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.
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.
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.
$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.
ASKER
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...
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";
}
foreach (<D:/My Documents/My Music/New2/*>) {
next if -d;
print $_;
if (-f) {
print " f ";
print -s if defined -s;
}
print "\n";
}
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.
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