Reputation: 18147
I have a folder structure:
Powershell-build
Comp1
Build
Impl
Comp2
Build
Impl
My output folder should contain only the Build
folder contents like shown below. Actually I want to maintain the parent folder structure but only particular folder contents should be copied.
Power-outputbuild
Comp1
Build
Comp2
Build
How to achieve this?
Upvotes: 0
Views: 263
Reputation: 68253
Not tested:
$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
}
Upvotes: 1
Reputation: 920
Here is an example of how to do it on perl using File::Copy::Recursive
module:
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);
Upvotes: 1