复制-项目排除子文件夹 [英] Copy-item exclude Sub-folders
本文介绍了复制-项目排除子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在Copy-Item中使用Get-Child和-Exclude,但没有像我希望的那样排除它们
$exclude = "foldercommon"
Get-ChildItem "c: est" -Directory |
Where-Object{$_.Name -notin $exclude} |
Copy-Item -Destination 'C:ackup' -Recurse -Force
希望公用文件夹存在,但其中的内容不会被复制。
感谢您的帮助
推荐答案
我认为这应该可以满足您的需求:
$sourceFolder = 'C: est'
$destination = 'C:ackup'
$exclude = @("foldercommon") # add more folders to exclude if you like
# create a regex of the folders to exclude
# each folder will be Regex Escaped and joined together with the OR symbol '|'
$notThese = ($exclude | ForEach-Object { [Regex]::Escape($_) }) -join '|'
Get-ChildItem -Path $sourceFolder -Recurse -File |
Where-Object{ $_.DirectoryName -notmatch $notThese } |
ForEach-Object {
$target = Join-Path -Path $destination -ChildPath $_.DirectoryName.Substring($sourceFolder.Length)
if (!(Test-Path -Path $target -PathType Container)) {
New-Item -Path $target -ItemType Directory | Out-Null
}
$_ | Copy-Item -Destination $target -Force
}
希望能有所帮助
这篇关于复制-项目排除子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文