写个perl脚本,能把好几个文件夹里的文件都结合到一个文件夹里
时间: 2024-03-14 17:43:41 浏览: 24
好的,以下是一个简单的Perl脚本,可以将指定的多个文件夹内的文件合并到一个文件夹中:
```perl
#!/usr/bin/perl
use strict;
use warnings;
use File::Copy;
# 定义输入和输出文件夹路径
my $input_dir = '/path/to/input/folder'; # 输入文件夹路径
my $output_dir = '/path/to/output/folder'; # 输出文件夹路径
# 遍历所有输入文件夹
opendir(my $dh, $input_dir) || die "无法打开输入文件夹: $!";
while (readdir $dh) {
next if $_ eq '.' or $_ eq '..'; # 忽略 . 和 ..
my $filename = $_;
my $input_file = "$input_dir/$filename";
my $output_file = "$output_dir/$filename";
copy($input_file, $output_file) or die "无法复制文件 '$input_file': $!";
}
closedir $dh;
print "所有文件已经复制到输出文件夹 '$output_dir' 中。\n";
```
使用方法:
- 将上述代码保存为一个文件,例如 `merge_folders.pl`。
- 打开命令行终端或控制台,进入包含待合并文件夹的目录。
- 运行以下命令:
```
perl merge_folders.pl
```
其中 `$input_dir` 和 `$output_dir` 分别为待合并文件夹的路径和输出文件夹的路径,可以根据实际情况修改。此命令将会将指定的多个文件夹内的所有文件复制到一个名为 `$output_dir` 的输出文件夹中。