嘗試將照片批量重命名為給定的命名方案,同時使用以下代碼保持當前排序順序:
Get-ChildItem | ForEach-Object -begin { $count=1 } -process { rename-item $_ -NewName "MyNamingSchemeWithIndex-$count.jp2"; $count }
重命名效果很好,但是在某些情況下,順序會混亂。到目前為止,我無法確定只有某些運行不正確的任何給定原因,期望檔案是 jp2 而不是 jpeg。到目前為止,使用 jpeg 似乎每次都是正確的。
我嘗試添加排序物件
Get-ChildItem | Sort-Object | ForEach-Object -begin { $count=1 } -process { rename-item $_ -NewName "MyNamingSchemeWithIndex-$count.jp2"; $count }
但這似乎沒有幫助。
剛跑
Get-ChildItem | Sort-Object
雖然有效并顯示正確的順序。
uj5u.com熱心網友回復:
這個怎么樣?確保按名稱排序。排序物件也會阻塞,直到收集到整個串列,否則我會將 (get-childitem) 放在括號中,以便它首先完成。使用 -whatif 來顯示會發生什么,盡管它并不總是正確的。-verbose 會顯示真實的結果。
echo hi | set-content a,b,c
Get-ChildItem | sort-object name |
ForEach-object { $count=1 } {
rename-item $_ -NewName MyNamingSchemeWithIndex-$count.jp2 -whatif; $count }
What if: Performing the operation "Rename File" on target "Item:
C:\users\admin\foo\a Destination:
C:\users\admin\foo\MyNamingSchemeWithIndex-1.jp2".
What if: Performing the operation "Rename File" on target "Item:
C:\users\admin\foo\b Destination:
C:\users\admin\foo\MyNamingSchemeWithIndex-2.jp2".
What if: Performing the operation "Rename File" on target "Item:
C:\users\admin\foo\c Destination:
C:\users\admin\foo\MyNamingSchemeWithIndex-3.jp2".
uj5u.com熱心網友回復:
在沒有看到有問題的示例檔案名的情況下,最可能的問題是資源管理器和PowerShell對帶有數字的字串進行排序的方式不同。

按資源管理器中顯示的順序處理檔案(或檔案夾)的最可靠方法是讓資源管理器提供檔案名串列。這可以使用
名稱降序:

也尊重團體順序:

因此,對于實際處理,在重命名之前獲取完全限定路徑的有序串列,因為您不想使用不斷變化的源集合。
$Shell = New-Object -ComObject shell.application
$WinFolder = @($Shell.Windows())[0].Document.Folder
$FilePaths = @($WinFolder.Items()).Path
# Do whatever proceessing you want:
ForEach ($ItemPath in $FilePaths)
{
### Prcess file here
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/523421.html
標籤:电源外壳改名块
