Last active
August 29, 2015 14:05
-
-
Save guitarrapc/e78bbd4ddc07389e17d6 to your computer and use it in GitHub Desktop.
Copy Selected Extensions, and Directory Structure Only
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
# Copy "All Directory Structure" and "File" which Extension type is .bat | |
Copy d:\GitHub\valentia -Destination d:\fuga -Force -Recurse -Filter "*.bat" | |
# Copy "All Directory Structure" and "File" which Extension type is .md | |
Copy d:\GitHub\valentia -Destination d:\fuga -Force -Recurse -Filter "*.md" | |
# If you want to exclude specific file to copy | |
Get-ChildItem -Path $Destination -Recurse -File | where Name -in Readme_J.md | Remove-Item -Recurse | |
# search Folder which include file | |
$hoge = ls d:\fuga -Recurse -Directory | where {$_.GetFiles()} | |
# Remove All Empty (none file exist) folders | |
ls d:\fuga -Recurse -Exclude $hoge -Directory | Remove-Item -Recurse | |
#> | |
function Copy-StrictedFilterFileWithDirectoryStructure | |
{ | |
[CmdletBinding()] | |
param | |
( | |
[parameter( | |
mandatory = 1, | |
position = 0, | |
ValueFromPipeline = 1, | |
ValueFromPipelineByPropertyName = 1)] | |
[string] | |
$Path, | |
[parameter( | |
mandatory = 1, | |
position = 1, | |
ValueFromPipelineByPropertyName = 1)] | |
[string] | |
$Destination, | |
[parameter( | |
mandatory = 1, | |
position = 2, | |
ValueFromPipelineByPropertyName = 1)] | |
[string[]] | |
$Targets, | |
[parameter( | |
mandatory = 0, | |
position = 3, | |
ValueFromPipelineByPropertyName = 1)] | |
[string[]] | |
$Excludes | |
) | |
begin | |
{ | |
$list = New-Object 'System.Collections.Generic.List[String]' | |
} | |
process | |
{ | |
Foreach ($target in $Targets) | |
{ | |
# Copy "All Directory Structure" and "File" which Extension type is $ex | |
Copy-Item -Path $Path -Destination $Destination -Force -Recurse -Filter $target | |
} | |
} | |
end | |
{ | |
# Remove -Exclude Item | |
Foreach ($exclude in $Excludes) | |
{ | |
Get-ChildItem -Path $Destination -Recurse -File | where Name -like $exclude | Remove-Item | |
} | |
# search Folder which include file | |
$allFolder = Get-ChildItem $Destination -Recurse -Directory | |
$containsFile = $allFolder | where {$_.GetFiles()} | |
$containsFile.FullName ` | |
| %{ | |
$fileContains = $_ | |
$result = $allFolder.FullName ` | |
| where {$_ -notin $list} ` | |
| where { | |
$shortPath = $_ | |
$fileContains -like "$shortPath*" | |
} | |
$result | %{$list.Add($_)} | |
} | |
$folderToKeep = $list | sort -Unique | |
# Remove All Empty (none file exist) folders | |
Get-ChildItem -Path $Destination -Recurse -Directory | where fullName -notin $folderToKeep | Remove-Item -Recurse | |
} | |
} | |
Copy-StrictedFilterFileWithDirectoryStructure -Path d:\GitHub\valentia -Destination D:\fuga -Target *.bat, *.md -Exclude Readme_J.md | |
# Sample | |
# Copy-StrictedFilterFileWithDirectoryStructure -Path D:\GitHub\valentia -Destination D:\fuga -Targets *.bat, *.md | |
# Sample : -Exlude item will not exit in copied folder | |
# Copy-StrictedFilterFileWithDirectoryStructure -Path d:\GitHub\valentia -Destination D:\fuga -Targets *.bat, *.md -Excludes Readme*.md |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exclude Sample
before begin
Empty
after
Result
-Exclud Readme_J.md makes file removed from destination.