如何仅复制特定文件夹但保持父文件夹结构完整?

时间:2012-01-24 12:56:55

标签: perl powershell powershell-v2.0

我有一个文件夹结构:

Powershell-build
    Comp1
        Build
        Impl
    Comp2
        Build
        Impl

我的输出文件夹应该只包含Build文件夹内容,如下所示。实际上我想维护父文件夹结构,但只应复制特定的文件夹内容。

Power-outputbuild
    Comp1
        Build
    Comp2
        Build

如何实现这一目标?

2 个答案:

答案 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
         }