[英]Powershell array failing because of "Copy-Item : Illegal characters in path."
[英]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.