2013-08-06 102 views
1

所以我是新来的Perl,我有一个文件夹,而不是包含子文件夹也包含子文件夹等,所有这一整个子文件夹和subsubfolders文件是说“.psd”删除一个文件夹中的所有.psd文件

我试着研究如何删除它们,到目前为止这是我最好的刺(但我不确定它会做什么,我不想最终删除我的计算机上的每个.psd文件)。 ..我只是想删除在特定的文件夹我的桌面上的所有.PSD文件(或该文件夹的子文件夹等)

至今代码: unlink glob('*.psd');

+0

(又是我全新的perl,所以我如何设置我想要在哪个文件夹中删除所有这些.psd文件......它只是我保存.pl文件perlscript文档中的哪个文件夹?) –

回答

1

脚本生成,并在几秒钟的帮助下修改:

find2perl -type f -name '*.psd' 

#!/usr/bin/perl 

use strict; 
use warnings; 
use autodie; # abort execution with warning when "unlink" fails. 
use File::Find; 

find { 
    # do the "wanted" action for each file/directory 
    wanted => sub { 
    unlink $_ if -f $_ and /\.psd$/; 
    # -f tests that the entry is a normal file 
    # The regex /\.psd$/ tests that the filename has a .psd ending 
    }, 
}, $ARGV[0]; # take the start directory from command line 

调用像

$ perl the-script.pl /place/where/you/want/to/start 

这将在指定的目录中递归地工作。

+1

'find2perl'生成可怕的代码,不应该作为perl新手的一个例子。 'wanted'看起来应该像''如果使用'no_chdir'选项设置了-f $ _和/ \。psd $ /',则取消$ _的链接。 – amon

+0

确实,这是丑陋的,它不会通过'perlcritic -4' =)但它有效。 –

+0

随意修改我的帖子amon。 –

相关问题