所以我之前在這里問過關于幫助腳本將檔案從一個檔案夾復制和粘貼到另一個檔案夾。
但是,完成后,我發現一些檔案丟失了。我有 600,393 個檔案,但是當我檢查我的新檔案夾時,它只有 600,361 個檔案。
我認為它可能已被重復項覆寫,即使命名約定應該阻止這些問題。
這是腳本
$destfolder = '.\destfolder\'
Get-ChildItem -Recurse -File .\srcfolder\ |
Invoke-Parallel {
$_ | Copy-Item -Destination (
Join-Path $using:destfolder ($_.directory.parent.name, $_.directory.name, $_.name -join '-')
) -Verbose -WhatIf
}
(感謝 r/software、r/Techsupport 和 mklement0 上的大佬們)
那么有沒有辦法添加一個后綴,將 0 添加到與檔案夾中已有檔案同名的任何檔案的名稱中?
像 directory-subdirectory-0-filename.ext
編輯-問題是所有檔案都是只讀的而不是隱藏的,我不想要任何隱藏檔案。
uj5u.com熱心網友回復:
請注意,默認情況下
Get-ChildItem不包括隱藏專案,這至少可以解釋部分差異。- 使用
-Force開關來包括隱藏的專案。
- 使用
單獨/另外,您可以按如下方式處理名稱沖突:
$destfolder = '.\destfolder\'
Get-ChildItem -Force -Recurse -File .\srcfolder\ |
Invoke-Parallel {
$newName = Join-Path $using:destfolder ($_.directory.parent.name, $_.directory.name, $_.name -join '-')
# Try to create the file, but only if it doesn't already exist.
if ($null -eq (New-Item $newName -ErrorAction SilentlyContinue)) {
# File already exists -> create duplicate with GUID.
$newName = '-' (New-Guid)
}
$_ | Copy-Item -Destination $newName -Verbose
}
筆記:
對于多執行緒執行,將序列號分配給重復項將是一項艱巨的任務,因為每個執行緒都必須“保留”一個序列號并確保在復制到包含該編號的檔案完成之前沒有其他執行緒宣告它。
為避免此類挑戰,上述方法只是將一個
-加號 GUID附加到目標檔案名。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/525274.html
標籤:电源外壳复制粘贴复制项目
