我有一个文件夹结构:
Powershell-build
Comp1
Build
Impl
Comp2
Build
Impl
我的输出文件夹应该只包含Build
文件夹内容,如下所示。实际上我想维护父文件夹结构,但只应复制特定的文件夹内容。
Power-outputbuild
Comp1
Build
Comp2
Build
如何实现这一目标?
答案 0 :(得分:1)
以下是使用File::Copy::Recursive
模块在perl上执行此操作的示例:
use File::Copy::Recursive qw/dircopy/;
my $parentdir = 'Powershell'; # dirname
my $parentdir_copy = $parentdir.'_outputbuild'; # output dirname suffix
opendir(my $dir_handle, $parentdir)
or die("Can't open dir: $!");
my @dirs = grep { ! /^\.{1,2}$/ } # ignore parent '..' and current '.' dirs
readdir($dir_handle) or die("Can't read dir: $!");
for my $dirname (@dirs) {
my $builddir = $dirname."/Build"; # "Build" directory
if (-d $parentdir."/".$builddir) {
dircopy($parentdir."/".$builddir, $parentdir_copy."/".$builddir);
}
}
closedir($dir_handle);
答案 1 :(得分:1)
未经测试:
$source = 'C:\Powershell-build\'
$target = 'C:\Power-outputbuild\'
$keep = '\\Build$'
Get-ChildItem $source |
where {$_.psiscontainer} |
select -expand fullname |
where {$_ -match $keep} |
foreach {
$new_dir = $_ -replace [regex]::escape($source),$target
mkdir $new_dir
copy-item $_*.* -destination $new_dir
}