Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/e78bbd4ddc07389e17d6 to your computer and use it in GitHub Desktop.
Save guitarrapc/e78bbd4ddc07389e17d6 to your computer and use it in GitHub Desktop.
Copy Selected Extensions, and Directory Structure Only
<#
# 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
@guitarrapc
Copy link
Author

Simple 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


    Directory: D:\fuga


Mode         LastWriteTime Length Name    
----         ------------- ------ ----    
d----   8/7/2014  11:08 PM        valentia


    Directory: D:\fuga\valentia


Mode         LastWriteTime Length Name             
----         ------------- ------ ----             
d----   8/7/2014  11:08 PM        valentia         
-a---  5/16/2014  11:50 AM   2078 README.md        
-a---  5/16/2014  11:50 AM   2113 Readme_J.md      
-a---   8/1/2014   4:01 AM  18467 VersionHistory.md


    Directory: D:\fuga\valentia\valentia


Mode         LastWriteTime Length Name     
----         ------------- ------ ----     
d----   8/7/2014  11:08 PM        example  
d----   8/7/2014  11:08 PM        functions
d----   8/7/2014  11:08 PM        tools    


    Directory: D:\fuga\valentia\valentia\example


Mode         LastWriteTime Length Name     
----         ------------- ------ ----     
-a--- 11/19/2013  11:41 AM    219 readme.md


    Directory: D:\fuga\valentia\valentia\functions


Mode         LastWriteTime Length Name     
----         ------------- ------ ----     
-a--- 11/19/2013  11:41 AM    695 readme.md


    Directory: D:\fuga\valentia\valentia\tools


Mode         LastWriteTime Length Name       
----         ------------- ------ ----       
-a---  5/16/2014  11:50 AM    317 install.bat


PS D:\GitHub\valentia>

@guitarrapc
Copy link
Author

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