所以我是新來的Perl,我有一個文件夾,而不是包含子文件夾也包含子文件夾等,所有這一整個子文件夾和subsubfolders文件是說「.psd」刪除一個文件夾中的所有.psd文件
我試着研究如何刪除它們,到目前爲止這是我最好的刺(但我不確定它會做什麼,我不想最終刪除我的計算機上的每個.psd文件)。 ..我只是想刪除在特定的文件夾我的桌面上的所有.PSD文件(或該文件夾的子文件夾等)
至今代碼: unlink glob('*.psd');
所以我是新來的Perl,我有一個文件夾,而不是包含子文件夾也包含子文件夾等,所有這一整個子文件夾和subsubfolders文件是說「.psd」刪除一個文件夾中的所有.psd文件
我試着研究如何刪除它們,到目前爲止這是我最好的刺(但我不確定它會做什麼,我不想最終刪除我的計算機上的每個.psd文件)。 ..我只是想刪除在特定的文件夾我的桌面上的所有.PSD文件(或該文件夾的子文件夾等)
至今代碼: unlink glob('*.psd');
腳本生成,並在幾秒鐘的幫助下修改:
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
這將在指定的目錄中遞歸地工作。
'find2perl'生成可怕的代碼,不應該作爲perl新手的一個例子。 'wanted'看起來應該像''如果使用'no_chdir'選項設置了-f $ _和/ \。psd $ /',則取消$ _的鏈接。 – amon
確實,這是醜陋的,它不會通過'perlcritic -4' =)但它有效。 –
隨意修改我的帖子amon。 –
(又是我全新的perl,所以我如何設置我想要在哪個文件夾中刪除所有這些.psd文件......它只是我保存.pl文件perlscript文檔中的哪個文件夾?) –