使用 File::Find 忽略隐藏文件
我正在使用 file::find
遍历目录结构并将其打印出来,但在排除隐藏文件时遇到问题。这是我到目前为止所得到的:
find(\&todo, $start_dir);
sub todo
{
if ($_ =~ /^./)
{
print "hidden file $_\n";
}
else
{
if (-f $_) #check for file
{
file;
}
elsif (-d $_) #check for directory
{
directory($File::Find::dir);
}
else
{
print "ERROR: $_\n";
}
}
}
如果我删除 if ($_ =~ /^./)
检查,文件和目录工作正常,但添加此操作会将所有内容打印为隐藏文件。正如你所看到的,我只需要这个就可以在 unix 上工作。 有人能指出我正确的方向吗?
编辑:我忘记了前面的反斜杠。 - 应该是 if ($_ =~ /^./),但是 find() 是否有忽略隐藏文件/目录的默认方式? 谢谢!
I am using file::find
to walk a directory structure and print it out, but I am having trouble excluding hidden files. Here is what I have so far:
find(\&todo, $start_dir);
sub todo
{
if ($_ =~ /^./)
{
print "hidden file $_\n";
}
else
{
if (-f $_) #check for file
{
file;
}
elsif (-d $_) #check for directory
{
directory($File::Find::dir);
}
else
{
print "ERROR: $_\n";
}
}
}
If I remove the if ($_ =~ /^./)
check, the files and directories work fine, but adding this prints everything as a hidden file. As you can see, I only need this to work on unix.
Can anyone point me in the right direction?
EDIT: I forgot a backslash in front of the . - should be if ($_ =~ /^./), but does find() have a default way of ignoring hidden files/directories?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正则表达式中的
.
匹配任何字符;使用\.
来匹配文字.
。您可能应该了解正则表达式。.
in a regex matches any character; use\.
to match a literal.
. And you probably should learn about regexes.请参阅 geekosaur 以获取对您问题的解释。在这样的简单情况下,
substr
可能会更好调用而不是正则表达式:正则表达式是一个很棒的工具,但它们不应该是您工具箱中的唯一东西。
See geekosaur for an explanation of your problem. In a simple case like this,
substr
might be a better call than a regular expression:Regular expressions are a great tool but they shouldn't be the only thing in your toolbox.
/^./
是一个正则表达式。句点表示任何单个字符,因此您所说的是匹配以任何字符开头的任何字符串,并且几乎匹配所有文件名。您需要在句号前添加反斜杠,或使用
\Q
和 '\E'。\Q
禁用元字符匹配,这意味着它基本上消除了所有魔力,使所有内容都成为普通的字符串。在这种情况下,反斜杠会更好,但您可以想象尝试匹配更复杂的东西,并且\Q
和\E
会更好:这些会起作用:
The
/^./
is a regular expression. The period means any single character, so what you're saying is match any string that starts with any character, and that pretty much matches all file names.You need to put a backslash before the period, or use the
\Q
and '\E'. The\Q
disables matching on metacharacters which means it basically removes all magic and makes everything a plain ol' string. In this circumstance, the backslash would be better, but you can imagine trying to match something a bit more complex, and the\Q
and\E
would work better:Either of these will work:
如果您使用的是 Windows,perl 模块 Win32::File会告诉您文件是否隐藏。 ActivePerl 中默认安装 Win32::File。
不幸的是 Win32::File 没有附带任何示例,(我希望 cpan 向 PHP 站点等每个页面添加注释功能。当然有 Annocpan,但额外的点击意味着几乎没有人为其做出贡献)< /em>,但是这个线程会帮助 http://www.perlmonks.org/?node_id=194011
还有更好的面向对象Win32::File::Object 但它仅适用于文件,除非您应用此补丁:( https://rt.cpan.org/Public/Bug/Display .html?id=60735
结果看起来很奇怪,因为很多文件除了被隐藏或系统之外都是隐藏的,我最终放弃了,只是修改了它以忽略某些文件夹:
If you are on Windows the perl module Win32::File will tell you whether a file is hidden or not. Win32::File is installed by default in ActivePerl.
Unfortunately Win32::File doesn't come with any examples, (I wish cpan added a comment feature to every page like the PHP site. Sure there's Annocpan, but that extra click means almost no one ever contributes to it), but this thread will help http://www.perlmonks.org/?node_id=194011
There's also the nicer object oriented Win32::File::Object but it only works with files unless you apply this patch :( https://rt.cpan.org/Public/Bug/Display.html?id=60735
Results seem weird though as lots of files you don't except to be hidden or system turn out to be both. I gave up in the end and just hacked this to ignore certain folders: