File::Find make me mad

A

Alitaia

I write a simple perl, del.pl, to use File::Find to find *.ape and
delete them. I can find the *.ape but can not del outside the
directory, can del them within the directory, I dont know what's
wrong.

for example:
If directory structure like this:
---./layer1
|
/layer2
|
a.ape
b.ape


with the following code, within layer2
#del.pl ./ a.ape and b.ape can be deleted

but if under ./layer1,
#del.pl ./layer2 a.ape, b.ape can be found but can not del

what is wrong?



#!/usr/bin/perl -w
#name: del.pl
use strict;
use File::Find;

sub wanted {
$_ = $File::Find::name;
if ( /\.ape$/ ) {
my $file = $File::Find::name;
print "Found ape file: $file\n";
#system "shntool", "conv", "-o", "flac", $File::Find::name;
my $cnt = unlink $file;
print "\$cnt is $cnt\n";
}
}
my $dir = shift;
$dir ||=".";
find (\&wanted, $dir);
 
J

John W. Krahn

Alitaia said:
I write a simple perl, del.pl, to use File::Find to find *.ape and
delete them. I can find the *.ape but can not del outside the
directory, can del them within the directory, I dont know what's
wrong.

for example:
If directory structure like this:
---./layer1
|
/layer2
|
a.ape
b.ape

with the following code, within layer2
#del.pl ./ a.ape and b.ape can be deleted

but if under ./layer1,
#del.pl ./layer2 a.ape, b.ape can be found but can not del

what is wrong?

#!/usr/bin/perl -w
#name: del.pl
use strict;
use File::Find;

perldoc File::Find
[ SNIP ]
You are chdir()'d to $File::Find::dir when the function is called,
unless no_chdir was specified.

sub wanted {

If './layer1' is passed from the command line and the current file is
'./layer1/layer2/a.ape' then $File::Find::dir contains './layer1/layer2'
and $_ contains 'a.ape' and $File::Find::name contains
'./layer1/layer2/a.ape'.

$_ = $File::Find::name;

Now both $_ and $File::Find::name contain './layer1/layer2/a.ape'. Why
are you doing this?

if ( /\.ape$/ ) {
my $file = $File::Find::name;
print "Found ape file: $file\n";
#system "shntool", "conv", "-o", "flac", $File::Find::name;
my $cnt = unlink $file;

The current directory is './layer1/layer2' so unlink is looking for the
file in './layer1/layer2/layer1/layer2/a.ape' instead of the current
directory.

print "\$cnt is $cnt\n";
}
}
my $dir = shift;
$dir ||=".";
find (\&wanted, $dir);



John
 
T

Todd

Hi,

I got the simplest form for this questions :)

perl -MFile::Find -e '
find sub { unlink if /\.ape$/} , shift || "."
'

-Todd
 
R

Randal L. Schwartz

Alitaia> I write a simple perl, del.pl, to use File::Find to find *.ape and
Alitaia> delete them. I can find the *.ape but can not del outside the
Alitaia> directory, can del them within the directory, I dont know what's
Alitaia> wrong.

In addition to the other answers, consider this, using my File::Finder
from the CPAN:

use File::Finder;
File::Finder->name('*.ape')->eval(sub { unlink })->in('.');

There. Done.
 
D

Dr.Ruud

Todd schreef:
I got the simplest form for this questions :)

perl -MFile::Find -e '
find sub { unlink if /\.ape$/} , shift || "."
'

perl -wle'
$p = shift || ".";
print for <$p/*.ape>
' optional/path

(replace "print" by "unlink", or even "print and unlink")
 
A

Alitaia

Alitaia said:
I write a simple perl, del.pl, to use File::Find to find *.ape and
delete them. I can find the *.ape but can not del outside the
directory, can del them within the directory, I dont know what's
wrong.
for example:
If directory structure like this:
---./layer1
|
/layer2
|
a.ape
b.ape
with the following code, within layer2
#del.pl ./ a.ape and b.ape can be deleted
but if under ./layer1,
#del.pl ./layer2 a.ape, b.ape can be found but can not del
what is wrong?
#!/usr/bin/perl -w
#name: del.pl
use strict;
use File::Find;

perldoc File::Find
[ SNIP ]
You are chdir()'d to $File::Find::dir when the function is called,
unless no_chdir was specified.
sub wanted {

If './layer1' is passed from the command line and the current file is
'./layer1/layer2/a.ape' then $File::Find::dir contains './layer1/layer2'
and $_ contains 'a.ape' and $File::Find::name contains
'./layer1/layer2/a.ape'.
$_ = $File::Find::name;

Now both $_ and $File::Find::name contain './layer1/layer2/a.ape'. Why
are you doing this?
if ( /\.ape$/ ) {
my $file = $File::Find::name;
print "Found ape file: $file\n";
#system "shntool", "conv", "-o", "flac", $File::Find::name;
my $cnt = unlink $file;

The current directory is './layer1/layer2' so unlink is looking for the
file in './layer1/layer2/layer1/layer2/a.ape' instead of the current
directory.
print "\$cnt is $cnt\n";
}
}
my $dir = shift;
$dir ||=".";
find (\&wanted, $dir);

John

Got it. Thanks you all! :)
 
D

Dr.Ruud

Peter J. Holzer schreef:
Dr.Ruud:

That doesn't search subdirectories.

Ah yes, totally forgot about that part. I was actually trying to come up
with an IO::All example, but that took me too much time, so I did the
glob instead.

perl -MIO::All -wle'
$_->unlink for io(shift || ".")->filter(sub{/\.ape$/})->All_Files
'

(which doesn't try to unlink directories named *.ape)

I must admit that I was quite disappointed when I found that
print for io(".")->All_Files(/\.ape$/)
didn't work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top