如何在Perl中读取多个目录并读取子目录的内容?
我有一个文件夹,里面有很多子文件夹。 在这些子文件夹中,我有许多要读取的 .html 文件。 我编写了以下代码来做到这一点。 它会打开父文件夹以及第一个子文件夹,并且仅打印一个 .html 文件。 它显示错误:
NO SUCH FILE OR DIRECTORY
我不想更改整个代码。 对现有代码的任何修改都会对我有好处。
use FileHandle;
opendir PAR_DIR,"D:\\PERL\\perl_programes\\parent_directory";
while (our $sub_folders = readdir(PAR_DIR))
{
next if(-d $sub_folders);
opendir SUB_DIR,"D:\\PERL\\perl_programes\\parent_directory\\$sub_folders";
while(our $file = readdir(SUB_DIR))
{
next if($file !~ m/\.html/i);
print_file_names($file);
}
close(FUNC_MODEL1);
}
close(FUNC_MODEL);
sub print_file_names()
{
my $fh1 = FileHandle->new("D:\\PERL\\perl_programes\\parent_directory\\$file")
or die "ERROR: $!"; #ERROR HERE
print("$file\n");
}
I have a folder and inside that I have many subfolders. In those subfolders I have many .html files to be read. I have written the following code to do that. It opens the parent folder and also the first subfolder and it prints only one .html file. It shows error:
NO SUCH FILE OR DIRECTORY
I dont want to change the entire code. Any modifications in the existing code will be good for me.
use FileHandle;
opendir PAR_DIR,"D:\\PERL\\perl_programes\\parent_directory";
while (our $sub_folders = readdir(PAR_DIR))
{
next if(-d $sub_folders);
opendir SUB_DIR,"D:\\PERL\\perl_programes\\parent_directory\\$sub_folders";
while(our $file = readdir(SUB_DIR))
{
next if($file !~ m/\.html/i);
print_file_names($file);
}
close(FUNC_MODEL1);
}
close(FUNC_MODEL);
sub print_file_names()
{
my $fh1 = FileHandle->new("D:\\PERL\\perl_programes\\parent_directory\\$file")
or die "ERROR: $!"; #ERROR HERE
print("$file\n");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您发布的代码看起来过于复杂。 查看 File::Find::Rule 你可以完成大部分繁重的工作用很少的代码。
我的意思是这不是很性感吗?!
一位用户评论说您可能希望仅使用 Depth=2 条目。
将应用此限制。
Your posted code looks way overcomplicated. Check out File::Find::Rule and you could do most of that heavy lifting in very little code.
I mean isn't that sexy?!
A user commented that you may be wishing to use only Depth=2 entries.
Will Apply this restriction.
您不会在
print_file_names()
函数中提取提供的$file
参数。它应该是:
顺便说一句,您在外循环中的
-d
测试看起来也错误。 您说的是next if -d ...
这意味着它将跳过目录的内部循环,这似乎与您的要求完全相反。 它起作用的唯一原因是因为您正在测试$file
这只是相对于路径的文件名,而不是完整的路径名。另请注意:
/
作为路径分隔符opendir($scalar, $path)
而不是opendir(DIR, $path)
注意:未经测试的代码如下:
You're not extracting the supplied
$file
parameter in theprint_file_names()
function.It should be:
Your
-d
test in the outer loop looks wrong too, BTW. You're sayingnext if -d ...
which means that it'll skip the inner loop for directories, which appears to be the complete opposite of what you require. The only reason it's working at all is because you're testing$file
which is only the filename relative to the path, and not the full path name.Note also:
/
as a path separatoropendir($scalar, $path)
instead ofopendir(DIR, $path)
nb: untested code follows:
请开始将:
放在所有脚本的顶部,它将帮助您避免此类问题并使您的代码更具可读性。
您可以在这里阅读更多相关信息:Perlmonks
Please start putting:
at the top of all your scripts, it will help you avoid problems like this and make your code much more readable.
You can read more about it here: Perlmonks
您将需要更改整个代码以使其健壮:
You are going to need to change the entire code to make it robust:
您是否考虑过使用
File::Find
Have you considered using
File::Find
这里有一种不需要使用File::Find的方法:
首先打开根目录,使用readdir将所有子文件夹的名称存储在一个数组中;
然后,使用 foreach 循环。 对于每个子文件夹,通过链接根目录和文件夹名称来打开新目录。 仍然使用 readdir 将文件名存储在数组中。
最后一步是编写用于处理此 foreach 循环内的文件的代码。
特别感谢我的老师给了我这个想法:)它真的很有效!
Here's one method which does not require to use File::Find:
First open the root directory, and store all the sub-folders' names in an array by using readdir;
Then, use foreach loop. For each sub-folder, open the new directory by linking the root directory and the folder's name. Still use readdir to store the file names in an array.
The last step is to write the codes for processing the files inside this foreach loop.
Special thanks to my teacher who has given me this idea :) It really worked well!