我正在嘗試使用 CSV 來批量上傳串列,但是影像 url 位于列中。
我使用 Amazon S3 來托管影像,使用 PowerShell 來檢索每個檔案的密鑰。但是我不確定如何按相關檔案進行分組,然后使用類似文本到列的內容進行拆分?
這些檔案具有一致的命名結構:
C2-123-1.JPG
C2-123-2.JPG
C2-123-3.JPG
C3-333-1.JPG
C3-333-2.JPG
在上面的例子中,C2-123 有三張照片,C2-333 只有兩張,所以我希望得到如下結果。
|圖片鏈接 1| 圖片鏈接2| 圖片鏈接3| 圖片鏈接 4| |C2-123-1.JPG| C2-123-2.JPG| C2-123-3.JPG| | |C3-333-1.JPG| C3-333-2.JPG| | |
uj5u.com熱心網友回復:
這應該有效,您應該替換$data從 AWS 獲得的輸出。
- 使用
$data來進行測驗:
$data = @'
C2-123-1.JPG
C2-123-2.JPG
C2-123-3.JPG
C3-333-1.JPG
C3-333-2.JPG
C3-333-4.JPG
C3-333-999.JPG
C3-456-2.JPG
C3-111-2.JPG
C3-999-4.JPG
'@ -split '\r?\n'
- 首先,按最后一個
-和.jpg擴展名之間的數字分組:
Count Name Group
----- ---- -----
2 1 {C2-123-1.JPG, C3-333-1.JPG}
4 2 {C2-123-2.JPG, C3-333-2.JPG, C3-456-2.JPG, C3-111-2.JPG}
1 3 {C2-123-3.JPG}
2 4 {C3-333-4.JPG, C3-999-4.JPG}
1 999 {C3-333-999.JPG}
- 然后得到
Group陣列的最大元素數 - 最后,使用一個
while回圈$max作為參考來轉換[pscustomobject]
# Group the files
$groups = $data | Group-Object {
[regex]::Match(
$_,
'(?i)(?<=\d-)(?<imagenum>\d )\.jpg$'
).Groups['imagenum'].Value
}
# Determine max number of elements
$max = $groups.Count | Measure-Object -Maximum
$index = 0
# Construct the object
$result = while($max.Maximum--)
{
$out = [ordered]@{}
$groups.ForEach({
$key = 'Image Link {0}' -f $_.Name
$out[$key] = $_.Group[$index]
})
[pscustomobject]$out
$index
}
結果將是:
PS /> $result | Format-Table
Image Link 1 Image Link 2 Image Link 3 Image Link 4 Image Link 999
------------ ------------ ------------ ------------ --------------
C2-123-1.JPG C2-123-2.JPG C2-123-3.JPG C3-333-4.JPG C3-333-999.JPG
C3-333-1.JPG C3-333-2.JPG C3-999-4.JPG
C3-456-2.JPG
C3-111-2.JPG
要查看regex說明,您可以使用https://regex101.com/r/kARr39/1
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/378530.html
下一篇:表存在時無法復制到不存在的表中
