写个perl脚本,能把一个文件夹里包含的所有文件夹里的文件都输出到另一个文件夹里
时间: 2023-08-10 12:03:24 浏览: 97
Java利用文件输入输出流实现文件夹内所有文件拷贝到另一个文件夹
5星 · 资源好评率100%
好的,这是一个简单的 Perl 脚本,可以实现将一个文件夹里包含的所有文件夹里的文件都输出到另一个文件夹里:
```perl
use strict;
use warnings;
use File::Copy;
my $source_dir = "/path/to/source/directory";
my $dest_dir = "/path/to/destination/directory";
opendir(my $dh, $source_dir) || die "Can't open directory: $source_dir: $!";
while (my $file = readdir $dh) {
next if ($file =~ /^\./); # skip hidden files/directories
my $source_file = "$source_dir/$file";
if (-d $source_file) { # if file is a directory, recurse into it
opendir(my $sub_dh, $source_file) || die "Can't open directory: $source_file: $!";
while (my $sub_file = readdir $sub_dh) {
next if ($sub_file =~ /^\./); # skip hidden files/directories
my $source_sub_file = "$source_file/$sub_file";
my $dest_sub_file = "$dest_dir/$sub_file";
copy($source_sub_file, $dest_sub_file) || die "Copy failed: $!";
}
closedir($sub_dh);
}
else { # if file is not a directory, copy it to destination directory
my $dest_file = "$dest_dir/$file";
copy($source_file, $dest_file) || die "Copy failed: $!";
}
}
closedir($dh);
print "All files copied successfully!\n";
```
你需要将 `$source_dir` 和 `$dest_dir` 的值替换为你的实际文件夹路径。然后在命令行中输入 `perl script.pl` 运行脚本即可。
阅读全文