是否有命令转到远程服务器并遍历路径并获取文件名



我正在编写一个perl脚本来获取远程服务器目录中存在的文件名,我找不到任何WMIC命令来遍历目录,是否有任何其他命令可以访问远程服务器并遍历特定路径以查找文件并检索文件名。

use strict;
use warnings;
use File::Find::Rule;
use File::Basename qw(basename);
my $path = "\\vmw2160\dir1"; 
my @full_pathes = File::Find::Rule->file->name('data.html')->in($path);print ".";
my @files = map { lc basename $_ } @full_pathes;
print foreach(@files);
my %file = map { $_ => 1 } @files;
print foreach(%file);     

您需要使用来自CPAN的File::Find::rule模块,其中Number::Compare 是一个依赖项。 请参阅脚本某些部分旁边的注释。

use strict;
use warnings;
use File::Find::Rule;
use File::Basename qw(basename);
my $path = "\\devicename\sharename";                  #Enter your path here, i.e Network drive
my $report = 'notfound.txt';        #This is just a log to tell you which files you searched for does not exist on the drive
print 'Enter file that contains list of files to search: ';
my $expected = <STDIN>;
   chomp $expected;
   open(my $fh, '<', $expected) or die "Could not open '$expected' $!n";
   open(my $out, '>', $report) or die "Could not open '$report' $!n";
my @full_pathes = File::Find::Rule->file->name('*')->in($path);
my @files = map { lc basename $_ } @full_pathes;
my %file = map { $_ => 1 } @files;
while (my $name = <$fh>) {
    chomp $name;
    if ($file{lc $name}) {  
        print "$name foundn";
    } else {
        print $out "$namen";
    }
}
close $out;
close $fh;

然后创建一个包含要搜索的文件列表的文件。让我们称之为myfiles.txt并以列表形式输入文件:

filename1.txt
filename2.pdf
filename3.bat

然后运行脚本,并根据请求输入文件名myfiles.txt提示符并输入。

编辑修改了代码以采用 UNC 路径。

相关内容

  • 没有找到相关文章

最新更新