使用Perl我想使用SFTP筛选名称与特定模式匹配的文件

时间:2018-09-20 13:06:34

标签: perl sftp

我尝试了以下代码段,但它始终获取当前目录中存在的所有文件的名称。

my %args = ("user" => $user, "password" => $pass, "warn" => 0 );

$con= Net::SFTP->new($server, %args);

@files = $con->ls($dir, wanted =>"*.csv");

2 个答案:

答案 0 :(得分:1)

似乎ls方法没有wanted选项。但是,您可以传递回调。这是一个使用Text::Glob和回调函数wanted()的示例:

use strict;
use warnings;
use Net::SFTP;
use Text::Glob qw( match_glob );

my $con= Net::SFTP->new('host', user => 'user', password => 'pass');
my $dir = '.';  # specify the directory

my @files;    # store filenames here
$con->ls($dir, sub { wanted( $_[0], '*.csv') } );

say for @files;  # print the matched filenames

sub wanted {
    my ( $info, $pat ) = @_;

    my $filename = $info->{filename};

    if (match_glob( $pat, $filename ) ) {
        push @files, $filename;
    }
}

答案 1 :(得分:0)

您可以在Net :: SFTP :: Foreign模块中以这种方式进行操作。我相信Net :: SFTP也应如此。

<html>
<head>



    <title>pie chart</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <style type="text/css">
        .box{
            width: 600px;
            margin: 0 auto;
            border: 1px solid #ccc;
        }
    </style>


    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>



    <script type="text/javascript">
        var analytics = <?php echo $categoryname;  ?>

        google.charts.load('current', {'packages':['corechart']});

        google.charts.setOnLoadCallback(drawChart);

        function drawChart()
        {
            var data = google.visulization.arrayToDataTable(analytics);

            var options = {
                title : 'Presentage of Categories'
            };

            var chart = new google.visulization.PieChart(document.getElementById('pie_chart'));
            chart.draw(data,options);
        }
    </script>
</head>
<body>
<div class="container">
    <h3 align="center">Category Chart</h3>
    <br>

    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-heading">Percentage of Categoryies</h3>
        </div>
        <div class="panel-body">
            <div id="pie_chart" style="width:750px; height: 450px;">

            </div>
        </div>
    </div>
</div>


</body>

</html>