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 |
Exclude Sample
before begin
Empty
PS D:\GitHub\valentia> ls d:\fuga -Recurse
PS D:\GitHub\valentia>
after
PS D:\GitHub\valentia> ls d:\fuga -Recurse
Result
-Exclud Readme_J.md makes file removed from destination.
Directory: D:\fuga
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2014/08/08 1:46 valentia
Directory: D:\fuga\valentia
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2014/08/08 1:46 valentia
-a--- 2014/05/16 8:07 2078 README.md
-a--- 2014/07/31 15:20 17960 VersionHistory.md
Directory: D:\fuga\valentia\valentia
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2014/08/08 1:46 example
d---- 2014/08/08 1:46 functions
d---- 2014/08/08 1:41 Tools
Directory: D:\fuga\valentia\valentia\example
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2014/02/14 4:37 219 readme.md
Directory: D:\fuga\valentia\valentia\functions
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2014/02/14 4:37 695 readme.md
Directory: D:\fuga\valentia\valentia\Tools
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2014/05/16 5:37 317 install.bat
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple Sample
before begin
Empty
after
Result