写个perl脚本,提取几个不同文件夹里的同名文件到新文件夹里
时间: 2024-03-31 18:38:03 浏览: 20
好的,以下是一个简单的 Perl 脚本,可以提取同名文件到新文件夹中:
```perl
use strict;
use warnings;
use File::Copy;
# 设置源文件夹和目标文件夹路径
my $source_dir1 = '/path/to/source/dir1';
my $source_dir2 = '/path/to/source/dir2';
my $target_dir = '/path/to/target/dir';
# 获取源文件夹中的所有文件
opendir(my $dh1, $source_dir1) or die "Can't open $source_dir1: $!";
opendir(my $dh2, $source_dir2) or die "Can't open $source_dir2: $!";
my @files1 = readdir($dh1);
my @files2 = readdir($dh2);
closedir($dh1);
closedir($dh2);
# 遍历文件夹中的所有文件
foreach my $file1 (@files1) {
foreach my $file2 (@files2) {
# 如果两个文件同名,拷贝到目标文件夹中
if ($file1 eq $file2) {
my $source_file1 = "$source_dir1/$file1";
my $source_file2 = "$source_dir2/$file2";
my $target_file = "$target_dir/$file1";
copy($source_file1, $target_file) or die "Copy failed: $!";
copy($source_file2, $target_file) or die "Copy failed: $!";
}
}
}
print "Done!\n";
```
你需要将 `$source_dir1` 和 `$source_dir2` 替换为你实际的源文件夹路径,将 `$target_dir` 替换为你想要保存文件的目标文件夹路径。通过运行该脚本,同名文件将被复制到新文件夹中。
相关推荐
![pl](https://img-home.csdnimg.cn/images/20210720083646.png)
![pl](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)