powershell - 复制项目排除子文件夹
问题描述
试图让我的复制项复制目录中除子文件夹之外的所有内容。我能够在文件夹和文件中排除,但不能在子文件夹中排除。
我尝试在复制项中使用 get-children 和 -exclude 但没有像我希望的那样排除它们
$exclude = "folder\common"
Get-ChildItem "c:\test" -Directory |
Where-Object{$_.Name -notin $exclude} |
Copy-Item -Destination 'C:\backup' -Recurse -Force
希望公用文件夹将存在,但其中没有任何内容可以复制。
谢谢您的帮助
解决方案
我认为这应该做你需要的:
$sourceFolder = 'C:\test'
$destination = 'C:\backup'
$exclude = @("folder\common") # 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
}
希望有帮助
推荐阅读
- reactjs - 使用 axios 向 elasticsearch 发送请求以映射卡片
- python - Discord Bot 上的循环功能不起作用
- powerbi - 计算连续行之间的百分比差异 - Power BI
- flutter - 参数“问题”的值不能为“空”,因为它的类型为“列表”
- javascript - 如何将base64格式的文件推送到数组中?
- r - 如何绘制二分存在/不存在数据以补充时间序列数据
- opencv - 如何有效地将编辑和展平的 PDF 中的所有黑色矩形更改为白色?
- azure - 用于 Web api 应用服务的 Azure 可用区
- node.js - 如何使用节点 reactjs 进行服务器端渲染?
- r - R中的时间序列预测,具有多个具有多个时间观察的产品