File::Find make me mad

File::Find make me mad

am 03.12.2007 04:10:56 von 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);

Re: File::Find make me mad

am 03.12.2007 08:00:49 von krahnj

Alitaia wrote:
>
> 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
--
use Perl;
program
fulfillment

Re: File::Find make me mad

am 03.12.2007 10:36:24 von xueweizhong

Hi,

I got the simplest form for this questions :)

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

-Todd

Re: File::Find make me mad

am 03.12.2007 17:15:12 von merlyn

>>>>> "Alitaia" == Alitaia writes:

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.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

Re: File::Find make me mad

am 04.12.2007 00:55:02 von rvtol+news

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")

--
Affijn, Ruud

"Gewoon is een tijger."

Re: File::Find make me mad

am 04.12.2007 02:32:22 von Alitaia

On Dec 3, 3:00 pm, "John W. Krahn" wrote:
> Alitaia wrote:
>
> > 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
> --
> use Perl;
> program
> fulfillment

Got it. Thanks you all! :)

Re: File::Find make me mad

am 04.12.2007 11:03:33 von bugbear

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

Yeah. It's in the doc, and due to backwards
compatibility cannot be changed.

But it is weird and horrid, and (in particular)
not the same as find(1).

BugBear (who was also fooled for a time)

Re: File::Find make me mad

am 04.12.2007 21:13:37 von hjp-usenet2

On 2007-12-03 23:55, Dr.Ruud wrote:
> 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

That doesn't search subdirectories.

hp

Re: File::Find make me mad

am 05.12.2007 02:42:37 von rvtol+news

Peter J. Holzer schreef:
> Dr.Ruud:
>> Todd:

>>> 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
>
> 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.

--
Affijn, Ruud

"Gewoon is een tijger."