[英]Modifying a Perl script which has an error handling spaces in files
我正在嘗試使用此處以Perl編寫的腳本,但該腳本會出錯。 我已經聯系過作者,但尚未回復。 我希望你能幫助我。
錯誤如下:
錯誤:無法將/home/bruno321/.config/Clementine/albumcovers/271b13967f57caba893b366730337fb03439c60a.jpg復制到/ media / mp3_1 / Musica / Amarok / Two%20Lone%20Swordsmen / A%20Bag%20of%20Blue%20Sparks /。 錯誤:文件或目錄不存在
我認為錯誤在於處理空格,因為在沒有空格的目錄中它可以正常運行,但是正如我認為腳本打算執行的那樣,它不會將文件重命名為“ cover.jpg”(而且我當然需要這樣做)。
#!/usr/bin/perl
use strict;
use warnings;
use Carp;
use DBI;
use strict;
use Data::Dumper;
use File::Basename;
use File::Copy;
my $db_filename = shift(@ARGV);
$db_filename
|| croak
"missing mandatory param: sqlite filename\n(try ~/.config/Clementine/clementine.db\n";
if ( !-f $db_filename ) {
croak "no such database file: $db_filename\n";
}
my $force_rewrite = shift(@ARGV) || 0;
my $dbh =
DBI->connect( "dbi:SQLite:$db_filename", q{}, q{},
{ 'RaiseError' => 1, 'AutoCommit' => 1 } );
my $query = q{SELECT artist,album, art_automatic,art_manual,filename
FROM songs WHERE art_manual IS NOT NULL GROUP BY(album) ORDER BY artist};
my $sth = $dbh->prepare($query);
$sth || croak 'prepare error: ' . $dbh->errstr . "\n";
$sth->execute || croak 'execute error: ' . $dbh->errstr . "\n";
my %treated = ();
while ( my $res = $sth->fetchrow_hashref ) {
( undef, my $dest_dir, undef ) = fileparse( $res->{'filename'} );
next if $treated{$dest_dir};
$treated{$dest_dir}++;
# strip leading file:// -> should do this better
$dest_dir =~ s/^file:\/\///g;
my $dest_file = $dest_dir . 'cover.jpg';
# unless we are given overwrite option, skip dirs that have covers
if ( !$force_rewrite ) {
next if ( -f $dest_file );
}
#print "CMD: cp $res->{'art_manual'} $dest_file\n";
copy( $res->{'art_manual'}, $dest_dir )
|| printf( "ERROR: unable to copy %s to %s. error: %s\n",
$res->{'art_manual'}, $dest_dir, $! );
}
exit;
顯然, $dest_dir
是URL編碼的,您需要將其設為原義。 換句話說,修剪掉file://
前綴后,您需要將%NN
十六進制轉義符解碼為相應的文字字符。 請參閱《 使用Perl》,如何在Web上解碼或創建這些%編碼? 基本上,你想
use URI::Escape 'uri_unescape';
接着
$dest_dir = uri_unescape($dest_dir);
此外,您正在創建$dest_file
但隨后將其忽略,而是將文件復制到$dest_dir
。
作為另一個樣式問題,警告和錯誤應發給stderr,而不是stdout。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.