免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2754 | 回复: 7
打印 上一主题 下一主题

邪门的File::Find, 请教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-12-02 18:22 |只看该作者 |倒序浏览
新学perl请教各位:

下面这段代码使用File::Find模块找到*.ape文件,然后将其删除。但是奇怪的是如果是

#./delape.pl ./ 则能正常工作。

但是,如果跨目录,比如,
#./delape.pl ./temp/ 则能找到*.ape文件,但是无法删除。这是怎么回事?


#!/usr/bin/perl -w
#name: delape.pl

use strict;
use File::Find;

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

论坛徽章:
0
2 [报告]
发表于 2007-12-03 11:02 |只看该作者
加了绝对路径后就能删除了

论坛徽章:
0
3 [报告]
发表于 2007-12-03 11:17 |只看该作者

回复 #2 liang573728 的帖子

高手,还真是,但是好像unlink并不需要绝对路径呀,为什么?

论坛徽章:
0
4 [报告]
发表于 2007-12-03 15:18 |只看该作者
要的,最终的操作都需要生成绝对路径来的。

论坛徽章:
0
5 [报告]
发表于 2007-12-04 09:35 |只看该作者
这是在newsgroup中别人回答我的,希望对大家有启发。

Alitaia           
View profile
         More options Dec 3, 11:10 am
Newsgroups: comp.lang.perl.misc
From: Alitaia <walter.newsgr...@gmail.com>
Date: Sun, 2 Dec 2007 19:10:56 -0800 (PST)
Local: Mon, Dec 3 2007 11:10 am
Subject: File::Find make me mad
Reply | Reply to author | Forward | Print | Individual message | Show original | Remove | Report this message | Find messages by this author
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);

    Reply    Reply to author    Forward  
               
               
               
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
        
               
John W. Krahn           
View profile
         More options Dec 3, 3:00 pm
Newsgroups: comp.lang.perl.misc
From: "John W. Krahn" <kra...@telus.net>
Date: Mon, 03 Dec 2007 07:00:49 GMT
Local: Mon, Dec 3 2007 3:00 pm
Subject: Re: File::Find make me mad
Reply | Reply to author | Forward | Print | Individual message | Show original | Report this message | Find messages by this author

- Hide quoted text -
- Show quoted text -
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

    Reply    Reply to author    Forward       Rate this post: Text for clearing space
Cancel
               
               
Send  Discard
               
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
               
From:         
Alitaia <walter.newsgroup@gmail.com>
Newsgroups:         
Cc:         
Followup To:         
        
Add Cc | Add Followup-to | Edit Subject         
Subject:         
               
Send  Discard
        
               
               
Your post was successful
               
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
        
               
Todd           
View profile
         More options Dec 3, 5:36 pm
Newsgroups: comp.lang.perl.misc
From: Todd <ueweizh...@gmail.com">xueweizh...@gmail.com>
Date: Mon, 3 Dec 2007 01:36:24 -0800 (PST)
Local: Mon, Dec 3 2007 5:36 pm
Subject: Re: File::Find make me mad
Reply | Reply to author | Forward | Print | Individual message | Show original | Report this message | Find messages by this author
Hi,

I got the simplest form for this questions

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

-Todd

    Reply    Reply to author    Forward       Rate this post: Text for clearing space
               
               
               
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
        
               
Randal L. Schwartz           
View profile
         Hide options Dec 4, 12:15 am
Newsgroups: comp.lang.perl.misc
From: mer...@stonehenge.com (Randal L. Schwartz)
Date: Mon, 03 Dec 2007 08:15:12 -0800
Local: Tues, Dec 4 2007 12:15 am
Subject: Re: File::Find make me mad
Reply | Reply to author | Forward | Print | Individual message | Show original | Report this message | Find messages by this author

>>>>> "Alitaia" == Alitaia  <walter.newsgr...@gmail.com> 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
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>;
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

    Reply    Reply to author    Forward       Rate this post:
               
               
               
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
        
               
Dr.Ruud           
View profile
         More options Dec 4, 7:55 am
Newsgroups: comp.lang.perl.misc
From: "Dr.Ruud" <rvtol+n...@isolution.nl>
Date: Tue, 4 Dec 2007 00:55:02 +0100
Local: Tues, Dec 4 2007 7:55 am
Subject: Re: File::Find make me mad
Reply | Reply to author | Forward | Print | Individual message | Show original | Report this message | Find messages by this author
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.

[ 本帖最后由 alitalia 于 2007-12-4 09:38 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2007-12-05 11:13 |只看该作者
请教
  1. $p = shift || ".";
复制代码

这个是什么意思?
我知道最终的结果是将在程序名后的输入取出,但是这里shift || ".";具体是什么意思呢?

论坛徽章:
0
7 [报告]
发表于 2007-12-05 11:26 |只看该作者
明白了,shift取出参数,如果参数为空则返回.,即当前目录

论坛徽章:
0
8 [报告]
发表于 2007-12-05 11:53 |只看该作者
这个module能支持查找多少天前修改的文件么?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP