使用Perl :: SFTP :: Foreign我试图计算具有特定文件句柄的文件数。
结果的用法是在代码中稍后重命名命令期间逐步对文件进行编号,以便第一个zip文件重命名为filename.zip.bak,然后是filename.zip.bak2,然后是filename.zip.bak3
my $file = "filename.zip"
$reName = ".bak";
@rls = $sftp->ls('.', wanted => qr/$file.*/ );
$rls_size = @rls;
$fileReName = $file . $reName;
if ( $rls_size > 1 ) {
$fileReName = $fileReName . $rls_size ;
}
unless ( $sftp->rename($file, $fileReName) ) {
Print "Error Renaming $file to $fileReName. $@ $!\n");
}
我很抱歉格式化。代码不断返回数组中的一个条目,以便不指定超过1的数字,即使通常在计数中选择了2个或更多。希望语句和给定正则表达式期望的格式是否有问题?
以下是用于执行相同功能的旧Net :: FTP代码:
sub process_file($)
{ my $file = $_[0];
if($reName)
{
@rls = $ftp->ls($file."*");
$rls_size = @rls;
$fileReName = $file . $reName;
if($rls_size > 1){ $fileReName = $fileReName . $rls_size; }
unless ($ftp->rename($file,$fileReName))
{ PrintLog (0, "Error Renaming $file to $fileReName. $@ $!\n");
$error =101;
exit 101;
}else{ PrintLog (7, "Renamed File $file to $fileReName.\n"); }
}
else{
unless ($ftp->delete($file))
{ PrintLog (0, "Error deleting $file. $@ $!\n");
$error =101;
exit 101;
}else{ PrintLog (7, "Deleted $file.\n"); }
}
}
答案 0 :(得分:-1)
If you're really using Net::SFTP::Foreign
then this line is wrong.
@rls = $sftp->ls('.',wanted=> qr/$file.*/ );
The documentation says that the ls
method returns a reference to an array of hashes
答案 1 :(得分:-1)
通过将命令从ls()更改为使用find(),它已更正了问题。具体来说,以下代码已被替换
这
@rls = $sftp->ls('.', wanted => qr/$file.*/ );
要
@rls = $sftp->find('.',wanted=> qr/^$file.*/ );
这已经成功生成了预期大小的数组。这似乎是ls(单个哈希数组)与find(哈希列表)之间的差异。