繁体   English   中英

在Powershell中具有排除文件和文件夹的Copy-Item

[英]Copy-Item with exclude files and folders in powershell

我有个问题。 我想将一个目录的内容复制到另一个目录并取得一些进展。 但是,此刻我陷入困境,因为如果我可以定义要排除的文件数组,则文件夹数组有问题。 所以数组看起来像:

$excludeFiles = @('app.config', 'file.exe')
$excludeFolders = @('Logs', 'Reports', 'Backup', 'Output')

当$ excludeFolders数组中只有一项时,它可以工作,但是如果我添加多个项目,它将复制所有文件夹而不排除它们。

脚本我有:

Get-ChildItem -Path $binSolutionFolder -Recurse -Exclude $excludeFiles | 
where { $excludeFolders -eq $null -or $_.FullName.Replace($binSolutionFolder, "") -notmatch $excludeFolders } |
Copy-Item -Destination {
    if ($_.PSIsContainer) {
        Join-Path $deployFolderDir $_.Parent.FullName.Substring($binSolutionFolder.length -1)
    }
    else {
        Join-Path $deployFolderDir $_.FullName.Substring($binSolutionFolder.length -1)
    }
} -Force -Exclude $excludeFiles

其中$ binSolutionFolder是源,而$ deployFolderDir是目标。 文件工作正常,但是使用文件夹时,我的想法已用尽。

-notmatch使用正则表达式模式而不是集合。 要与单词集合匹配,可以使用-notin $excludedfolders ,但是如果路径包含2级以上的文件夹或仅包含简单的\\则测试将失败。

我将使用-notmatch ,但首先创建一个正则表达式模式来检查所有文件夹。 例如:

$excludeFiles = @('app.config', 'file.exe') 
$excludeFolders = @('Logs', 'Reports', 'Backup', 'Output','Desktop')
$excludeFoldersRegex = $excludeFolders -join '|'

Get-ChildItem -Path $binSolutionFolder -Recurse -Exclude $excludeFiles | 
where { $_.FullName.Replace($binSolutionFolder, "") -notmatch $excludeFoldersRegex } |
.....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM