大家好,2022 年最好的祝愿!
我有以下 CSV 檔案:
Name,EmployeeID,File
JohnDoechebox,0009001,ImageBig1.png
JohnDoechebox,0009001,ImageBig2.png
JohnDoechebox,0009001,ImageFlat1.jpg
JohnDoechebox,0009001,ImageFlat2.jpg
JaneJefferson,0009006,IntDoc.docx
JaneJefferson,0009006,IntDoc2.docx
JaneJefferson,0009006,IntImage.jpg
JaneJefferson,0009006,ExtImage.jpg
我想匯入 CSV 檔案,我可以用Import-CSV. 然后我想要foreach匯入的 CSV 檔案,以便決議所有行。
我已經測驗了一下,我想出了以下內容:
$Name = @()
$EmployeeID = @()
# Importing the CSV file and fill the array
Import-Csv -Path "C:\Temp\TestNew.csv" |`
ForEach-Object {
$Name = $_."Name"
$EmployeeID = $_."EmployeeID"
Write-Host "Your name is $($_.Name) and your Employee ID is $($_.EmployeeID)"
}
這按預期作業。所以現在我想構建它,以便我可以根據 EmployeeID 獲取該特定用戶的所有檔案(因為名稱可以重復,EmployeeID 始終是唯一的)并通過Write-Host. 像這樣:
Write-Host "Your name is $($_.Name) and your Employee ID is $($_.EmployeeID)" You have the following files:
稍后我還想對每個檔案執行一個操作以將其復制到某處。
任何幫助將不勝感激!提前致謝!
uj5u.com熱心網友回復:
這可以按如下方式完成
$list = import-csv 'C:\Temp\TestNew.csv' | Group-Object EmployeeID
foreach ($user in $list){
Write-Host "Your name is $($user.group.name[0]) and your Employee ID is $($user.Name) , You have the following files:"
$user.Group.file
}
uj5u.com熱心網友回復:
要獲取具有特定EmployeeID值的所有檔案,請使用Group-Objectcmdlet 按該特定列對 CSV 中的條目進行分組:
Import-Csv -Path "C:\Temp\TestNew.csv" |Group-Object EmployeeID |ForEach-Object {
# Get the whole list of file names
$groupOfFileNames = $_.Group |ForEach-Object File
# Pick the name from the first entry in the grouped list
$userName = $_.Group[0].Name
Write-Host "Your name is $($_.Name) and your Employee ID is $($_.EmployeeID)"
Write-Host "You have the following files: [$($groupOfFileNames -join ', ')]"
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/406816.html
標籤:
