windows 如何使用命令行将所有 .pdf 文件名打印到输出文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4515510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I print all .pdf files names to an output file with command line?
提问by Jake
This seems easy in Linux, but I'm trying to print the names of *.pdf
files within a directory and its subdirectories to an output file. I have Perl installed on my Windows machine.
这在 Linux 中似乎很容易,但我正在尝试将*.pdf
目录及其子目录中的文件名打印到输出文件中。我在我的 Windows 机器上安装了 Perl。
What's a simple way to do this?
有什么简单的方法可以做到这一点?
回答by boatcoder
Not much different than Linux.
与Linux没有太大区别。
dir *.pdf > fileyouwant.txt
If you only want the filenames, you can do that with
如果你只想要文件名,你可以用
dir/b *.pdf > fileyouwant.txt
If you also want subdirs,
如果你还想要子目录,
dir/s/b *.pdf > fileyouwant.txt
If you aren't in that directory to start with
如果您不在该目录中开始
dir/s/b C:\Path\*.pdf > fileyouwant.txt
回答by ysth
use strict;
use warnings;
use File::Find;
my $dirname = shift or die "Usage: use File::Find::Rule;
my $rule = File::Find::Rule->file()->name('*.pdf')->start('C:/Path/');
while (defined (my $pdf = $rule->match)) {
print "$pdf\n";
}
dirname >outputfile";
File::Find::find( sub {
print $File::Find::name, "\n" if $File::Find::name =~ /\.pdf\z/
}, $dirname );
回答by ephemient
File::Find::Ruleis often nicer to use than File::Find.
File::Find::Rule通常比File::Find更好用。
use File::Find::Rule;
print "$_\n" for File::Find::Rule->file()->name('*.pdf')->in('C:/Path/');
or simply
或者干脆
#!/usr/bin/perl
use File::Glob ':glob'; # Override glob built-in.
print join("\n",glob("*.pdf"));
回答by Jonathan Leffler
Using Perl, you should almost certainly be using the File::Findcore module.
使用 Perl,您几乎肯定会使用File::Find核心模块。