[英]Powershell Rename Files in a Directory Synchronously
我有一個腳本,可以在導入.CSV后從字段/列重命名目錄中的所有文件。 我的問題是PS正在異步而不是同步地重命名文件。 有沒有更好的方法來獲得我想要的結果?
當前文件名= 123456789.pdf
新文件名= $ documentID_ $ fileID
我需要新的文件名來重命名文件,以使腳本可行。
這是我的代碼(這是我的新手):
$csvPath = "C:\Users\dougadmin28\Desktop\Node Modify File Name App\test.csv"
$filePath = "C:\Users\dougadmin28\Desktop\Node Modify File Name App\pdfs"
$csv = Import-Csv $csvPath Select-Object -Skip 0
$files = Get-ChildItem $filePath
foreach ($item in $csv) {
foreach($file in $files) {
Rename-Item $file.fullname -NewName "$($item.DocumentID +"_"+ ($item.FileID)+($file.extension))" -Verbose
}
}
您可以嘗試使用工作流,這將允許您並行執行任務: https : //docs.microsoft.com/zh-cn/powershell/module/psworkflow/about/about_about_foreach-parallel?view=powershell-5.1
請記住,PowerShell工作流具有一些限制: https : //devblogs.microsoft.com/scripting/powershell-workflows-restrictions/
希望能幫助到你!
我認為同步意味着按順序進行,就像“一個接一個”一樣,這就是您的腳本現在正在做的。
如果您要異步或彼此獨立地說“並行”,可以考慮使用
Start-Job
,“ wait-job
和“ receive-job
。 最容易使用,但效率卻不高。 在某些cmdlet中,它還可以作為-AsJob
開關使用。 Runspaces
。 [最有效,但很難編碼] Workflows
[平衡,但有局限性]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.