Win32 Perl - 使用传递的目录参数区分文件和文件夹之间的区别

发布于 2024-09-16 04:56:01 字数 737 浏览 1 评论 0原文

我正在用 Perl 草莓写一个脚本。它需要做的第一件事是获取路径参数并获取该路径的目录列表,并且它需要能够区分文件和文件夹。我阅读了有关该主题的一些教程并编写了下面的脚本,但只有当我给它脚本当前所在的路径时它才有效。如果我给它任何其他路径,-f 和 -d 测试将​​不起作用。

编辑:澄清:如果我给它一个不同于它自己的路径,脚本确实会将所有文件和文件夹放入@thefiles,这只是 -f 和 -d 测试不起作用。

use Getopt::Long;

my $dir;

GetOptions('-d=s' => \$dir);

opendir(DIR, $dir) or die "BORKED";
@thefiles = readdir(DIR);
print DIR;
closedir(DIR);

@filez;
@dirz;

foreach $file (@thefiles){
 if (-f$file){
  push(@filez, $file);
 }
 if (-d$file){
  push(@dirz, $file);
 }
}

print "files: @filez \n";
print "Directories: @dirz \n";

这是一个屏幕截图: https://i.sstatic.net/RMmFz.jpg

希望有人可以帮助并非常感谢您的宝贵时间。 :)

I'm writing a script in perl strawberry. The first thing it needs to be able to do is take a path argument and get a directory listing for that path, and it needs to be able to distinguish between files and folders. I read some tutorials on the subject and wrote the script below, but it only works when I give it the path that the script is currently residing in. If I give it any other path, the -f and -d tests don't work.

EDIT: Clarification: The script DOES put all the files and folders into @thefiles if I give it a path other than it's own, it's just the -f and -d tests that don't work.

use Getopt::Long;

my $dir;

GetOptions('-d=s' => \$dir);

opendir(DIR, $dir) or die "BORKED";
@thefiles = readdir(DIR);
print DIR;
closedir(DIR);

@filez;
@dirz;

foreach $file (@thefiles){
 if (-f$file){
  push(@filez, $file);
 }
 if (-d$file){
  push(@dirz, $file);
 }
}

print "files: @filez \n";
print "Directories: @dirz \n";

Here's a screenshot: https://i.sstatic.net/RMmFz.jpg

Hope someone can help and thanks very much for your time. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心凉 2024-09-23 04:56:01

马丁·克莱顿告诉你你的代码不起作用的原因。

以下是使用 map 和一些更现代的 Perl 构造来修复它的方法:

use strict; 
use warnings; 
use Getopt::Long; 

my $dir; 

GetOptions('-d=s' => \$dir); 

opendir my $dh, $dir or die "BORKED: $!";
my @thefiles = map { "$dir/$_" } readdir $dh;
closedir $dh;

my @filez; 
my @dirz; 

for my $file (@thefiles) { 
    push @filez, $file if -f $file;
    push @dirz , $file if -d $file;
} 

print "files: @filez \n"; 
print "Directories: @dirz \n"; 

martin clayton told you the reason your code does not work.

Here is a way to fix it using map and some more modern Perl constructs:

use strict; 
use warnings; 
use Getopt::Long; 

my $dir; 

GetOptions('-d=s' => \$dir); 

opendir my $dh, $dir or die "BORKED: $!";
my @thefiles = map { "$dir/$_" } readdir $dh;
closedir $dh;

my @filez; 
my @dirz; 

for my $file (@thefiles) { 
    push @filez, $file if -f $file;
    push @dirz , $file if -d $file;
} 

print "files: @filez \n"; 
print "Directories: @dirz \n"; 
一抹苦笑 2024-09-23 04:56:01

这是因为 filetest -f-d 运算符使用相对路径,除非您提供绝对路径。 readdir 函数将返回在目录中找到的文件(和子目录...)名称,但不是完整路径。

来自文档

如果您打算进行文件测试
从 readdir 返回值,你会
最好在前面添加目录
问题。否则,因为我们没有
chdir 在那里,本来就是
测试错误的文件。

It's because the filetest -f and -d operators use a relative path unless you provide an absolute one. The readdir function will return the file (and subdirectory...) names found in the directory, but not the full paths.

From the docs:

If you're planning to filetest the
return values out of a readdir, you'd
better prepend the directory in
question. Otherwise, because we didn't
chdir there, it would have been
testing the wrong file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文