如何在 Windows 上的 Perl 中获取最后修改的 ftp 目录? [英] How can I get the last modified ftp directory in Perl on Windows?
问题描述
通过我的@dir = $ftp->ls() 我可以得到所有目录的列表,但女巫一个是最新的,我如何过滤那个.我正在使用 Windows 操作系统,而那些目录来自 FTP.
By my @dir = $ftp->ls() i can get the list of all dir but witch one is latest how can i filter that one. I am using windows os and those dir is from FTP.
谢谢
推荐答案
对于措辞不慎的问题,您将得到一个快速而肮脏的黑客:
You'll get a qucik and dirty hack for your carelessly worded question:
第一:
假设您使用的是 Net::FTP
Assuming you are using Net::FTP
你必须打电话
$ftp->dir()
而不是
$ftp->ls()
获取长目录列表.
然后试试这个:
use feature "say";
use Net::FTP;
use Date::Parse;
$ftp = Net::FTP->new("ftp", Debug => 0)
or die "Cannot connect to some.host.name: $@";
$ftp->login("anonymous",'-anonymous@')
or die "Cannot login ", $ftp->message;
$ftp->cwd("/pub")
or die "Cannot change working directory ", $ftp->message;
@dir = $ftp->dir()
or die "ls()/dir() failed ", $ftp->message;
#map {say } @dir;
#Now parse the array of strings that dir() returned
#magic numbers to find substring with modif-date
my $start = 44;
my $len = 10;
@dir = map {$_->[0]} sort {$b->[1] <=> $a->[1]} map {[$_, str2time(substr($_, $start, $len))] } grep {/^d/} @dir;
$latest = $dir[0];
这只适用于这种格式的目录
This will work only for directories with this format
drwxr-xr-x 17 root other 4096 Apr 12 2010 software
但不是这样(注意:缺少年份)
but not with this (note:year missing)
drwxr-xr-x 36 root root 4096 Nov 29 09:14 home
代码也会忽略符号链接,例如:
The code will also ignore symbolic links such as this:
lrwxrwxrwx 1 root root 8 May 30 2011 i -> incoming
但它会给你一个开始.
map{} sort{} map {} @array;
构造被称为施瓦兹变换",并完成大部分工作.
construct is called a "Schwartzian transform", and does most of the work.
这篇关于如何在 Windows 上的 Perl 中获取最后修改的 ftp 目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!