到目前為止我所擁有的:
Get-ChildItem -Path "\\Networkdrive\Folder1\PROD- TEST\Successful" -Recurse |
Where-Object name -match '\w{10}_\w{3}' |
Move-Item -destination "\\Networkdrive\Folder1\Archive- TEST\Archive_PROD_TEST\Successful" -Verbose
在 : 里面\\Networkdrive\Folder1\PROD- TEST\Successful是看起來像這樣的檔案夾:9ce8eab8-cefe-4ec9-9158-ff40612d5c47然后是更多的檔案夾,這些檔案夾的標準是 10 個數字,帶下劃線 ( _),然后是 3 個數字。
9ce8eab8-cefe-4ec9-9158-ff40612d5c47如果任何子檔案夾包含 10 個字符后跟一個_然后和其他 3 個字符到的條件,我想移動檔案夾\\Networkdrive\Folder1\Archive- TEST\Archive_PROD_TEST\Successful,但我想單獨保留結構和檔案。
現在,代碼只是抓取里面的檔案夾然后移動它們,但不理會9ce8eab8-cefe-4ec9-9158-ff40612d5c47檔案夾。我也想9ce8eab8-cefe-4ec9-9158-ff40612d5c47被感動
uj5u.com熱心網友回復:
雖然不太清楚子檔案夾命名約定是什么(你談論的是numbers),所以下面我使用這個模式:\d{10}_\d{3}.
但是,如果這些是十六進制數字,則需要使用模式[0-9a-f]{10}_[0-9a-f]{3}
嘗試:
$sourcePath = '\\Networkdrive\Folder1\PROD- TEST\Successful'
$destination = '\\Networkdrive\Folder1\Archive- TEST\Archive_PROD_TEST\Successful'
$folderPattern = '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
$subdirPattern = '\d{10}_\d{3}' # if the name must match FULLY, use anchors: '^\d{10}_\d{3}$'
Get-ChildItem -Path $sourcePath -Directory | Where-Object {$_.Name -match $folderPattern} | ForEach-Object {
# capture the folders FullName for when we hit the catch block in case of error
$targetFolder = $_.FullName
if (@(Get-ChildItem -Path $targetFolder -Directory | Where-Object { $_.Name -match $subdirPattern }).Count) {
# one or more subfolders found with name matching the $subdirPattern, so move the folder
Write-Host "Moving folder '$targetFolder'"
try {
Move-Item -Path $targetFolder -Destination $destination -Force -ErrorAction Stop
}
catch {
Write-Warning "Error moving folder '$targetFolder':`r`n$($_.Exception.Message)"
}
}
}
uj5u.com熱心網友回復:
假設名為 的檔案夾是名為 的檔案夾XXXXXXXXX_XXX的直接子級XXXXXXX-XXXX-XXXX-XXXXXXXXXXXX,那么您可以:
- 找到與您想要的模式匹配的檔案夾,然后
- 獲取
Parent這些物件的屬性。
例如,我會使用這個:
Get-ChildItem -Path "\\Networkdrive\Folder1\PROD-TEST\Successful\*\*" -Directory |
Where-Object name -match '\w{10}_\w{3}' |
Get-ItemPropertyValue -Name Parent |
Move-Item -Destination "\\Networkdrive\Folder1\Archive- TEST\Archive_PROD_TEST\Successful" -Verbose
請注意,這種簡單的策略僅在XXXXXXXXXX_XXX目錄相對于需要移動的檔案夾始終處于相同深度時才有效,因為Parent讓您正好處于同一級別。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/316406.html
