简体   繁体   English

如何在Perl中打开多个文件夹并比较文件?

[英]How to open multiple folders and compare the files in Perl?

I have a parent_folder_1 and many sub folders inside that. 我有一个parent_folder_1和里面的许多子文件夹。 I also have parent_folder_2 and many sub folders inside that. 我也有parent_folder_2和里面的许多子文件夹。

Sub folders contains .d files. 子文件夹包含.d文件。

I have to get the number of parent_folders that the user wants to compare. 我必须获取用户想要比较的parent_folders的数量。

For example: 例如:

"How many parent_folders do you want to compare?" “您想比较几个parent_folders?”

If the user inputs 4, then the script should ask for paths of all the four parent_folders . 如果用户输入4,则脚本应询问所有四个parent_folders路径。

And, in one parent folder, inside one sub_folder, there is another folder called d folder which contains .d files. 并且,在一个子文件夹的一个父文件夹中,还有一个名为d folder ,其中包含.d文件。

I have to compare each .d file in all the four parent folders and have to put the results in an Excel spreadsheet as to which .d files are present in which parent folders. 我必须比较所有四个父文件夹中的每个.d文件,并且必须将结果放入Excel电子表格中,以了解哪些父文件夹中存在哪些.d文件。

If xxx.d sf present in parent folder1 and parent folder3 , then the output should be: 如果xxx.d SF存在于parent folder1parent folder3 ,则输出应该是:

xxx.d  parent folder1 #this space is for parent folder2 if present# parentfolder3

This is the idea: 这是个主意:

  • Use File::Find to traverse directories, sub-directories and files. 使用File :: Find遍历目录,子目录和文件。
  • In wanted() , check for: wanted() ,检查:

    -f $File::Find::name && $File::Find::name =~ /^.+\\.d$/ -f $ File :: Find :: name && $ File :: Find :: name =〜/^.+\\.d$/

  • If the above condition evaluates to false, return; 如果以上条件为假,则返回;否则为false。 else, compare the files and populate the spreadsheet. 否则,比较文件并填充电子表格。

U can try using 你可以尝试使用

#opening the directory
opendir (DIR, $directory) or die $!;
#reading the directory 
while (my $file = readdir(DIR)) {
print $file;}
#thus the giving the accessing the file in the way u would like to.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM