使用 Import-csv 并将列的多个值存储为变量

Using Import-csv and store multiple values of a column as a variable

提问人:cnjfo672 提问时间:11/2/2022 最后编辑:Ken Whitecnjfo672 更新时间:11/3/2022 访问量:936

问:

我有一个 csv 文件,其中有一个名为“art”的列:

sequence art last  first  address city st     zip
-------- --- ----  -----  ------- ---- --     ---
1        3S  Doe   John   123     Any  Street AnyTown
6        3S  Doe   John   128     Any  Street AnyTown
2        OG  Dow   Jane   124     Any  Street AnyTown
7        OG  Dow   Jane   129     Any  Street AnyTown
3        OGF Cool  Joe    125     Any  Street AnyTown
8        OGF Cool  Joe    130     Any  Street AnyTown
4        SLV Cool  Carol  126     Any  Street AnyTown
9        SLV Cool  Carol  131     Any  Street AnyTown
5        SMP Bravo Johnny 127     Any  Street AnyTown
10       SMP Bravo Johnny 132     Any  Street AnyTown

我正在寻找一种将“艺术”列的值存储到变量中的方法。 请耐心等待,因为我仍在学习 PowerShell。这是我目前拥有的代码:

If (Test-Path D:\mhWork\_Auto\_Print\c1478_tpl_snap\data\Art_cProofs.csn){
$source=Get-ChildItem "D:\mhWork\_Auto\_Print\c1478_tpl_snap\data\Art_cProofs.csn" | select  -Last 1
$artTag = 'D:\mhWork\_Auto\_Print\c1478_tpl_snap\data\Art_cProofs.csn'
$artTag1 = Import-Csv $artTag | Select-Object -ExpandProperty art -First 1
$artTag2 = Import-Csv $artTag | Select-Object -ExpandProperty art -Second 2
$artTag3 = Import-Csv $artTag | Select-Object -ExpandProperty art -Third 3
$artTag4 = Import-Csv $artTag | Select-Object -ExpandProperty art -Fourth 4
$artTag5 = Import-Csv $artTag | Select-Object -ExpandProperty art -Fifth 5
$JobID = "TPL_" + ($aJobNum)

If (Test-Path cProofs0001.pdf){
Rename-Item cProofs0001.pdf "$($JobID)_$($artTag1)_cProofs.pdf"}
If (Test-Path cProofs0002.pdf){
Rename-Item cProofs0002.pdf "$($JobID)_$($artTag2)_cProofs.pdf"}
If (Test-Path cProofs0003.pdf){
Rename-Item cProofs0003.pdf "$($JobID)_$($artTag3)_cProofs.pdf"}
If (Test-Path cProofs0004.pdf){
Rename-Item cProofs0004.pdf "$($JobID)_$($artTag4)_cProofs.pdf"}
If (Test-Path cProofs0005.pdf){
Rename-Item cProofs0005.pdf "$($JobID)_$($artTag5)_cProofs.pdf"}
}

正如您在“艺术”列中看到的那样,有 5 个不同的值。在我通过 $artTag 1 之前,代码工作正常。所需的结果将重命名以下文件。

cProofs0001.pdf - to - 123456_TPL_3S_cProofs.pdf
cProofs0002.pdf - to - 123456_TPL_OG_cProofs.pdf
cProofs0003.pdf - to - 123456_TPL_OGF_cProofs.pdf
cProofs0004.pdf - to - 123456_TPL_SLV_cProofs.pdf
cProofs0005.pdf - to - 123456_TPL_SMP_cProofs.pdf

我想我可能需要使用 get-content 或 for-each 循环。

PowerShell for-loop import-csv select-object ob-get-contents

评论

0赞 Theo 11/2/2022
硬编码的文件名等是怎么回事??该编号与列中的值有关吗?当你只能做一次时,为什么要重复 x 次?cProofs0001.pdfcProofs0002.pdfsequenceImport-Csv $artTag
0赞 cnjfo672 11/3/2022
硬编码的 filname 是我们的软件在文件被 10 条记录分解时对文件的命名。导入的文件通常有 10 条以上的记录。关于重复的“Import-CSV $artTag”问题,我仍在学习,不确定如何让 PowerShell 读取值的“art”列。

答:

1赞 AHaleIII 11/3/2022 #1
# load the entire csv into a variable
$csvFilepath = 'D:\mhWork\_Auto\_Print\c1478_tpl_snap\data\Art_cProofs.csn'
$csvContent = Import-Csv -Path $csvFilepath

# get the values of the art column like you did with -ExpandProperty
$artValues = $csvContent | Select-Object -ExpandProperty art

# de-duplicate the list
$artTags = $artValues | Group-Object | Select-Object -ExpandProperty Name

# use array indexing to reference values in list
$JobID = "TPL_" + ($aJobNum) # unclear where $aJobNum comes from

If (Test-Path cProofs0001.pdf){
Rename-Item cProofs0001.pdf "$($JobID)_$($artTags[0])_cProofs.pdf"}
If (Test-Path cProofs0002.pdf){
Rename-Item cProofs0002.pdf "$($JobID)_$($artTags[1])_cProofs.pdf"}
If (Test-Path cProofs0003.pdf){
Rename-Item cProofs0003.pdf "$($JobID)_$($artTags[2])_cProofs.pdf"}
If (Test-Path cProofs0004.pdf){
Rename-Item cProofs0004.pdf "$($JobID)_$($artTags[3])_cProofs.pdf"}
If (Test-Path cProofs0005.pdf){
Rename-Item cProofs0005.pdf "$($JobID)_$($artTags[4])_cProofs.pdf"}
}

我不清楚&等与列中的值之间的关系是什么。唯一确定与之关联的是.csv文件中行的顺序。这个顺序会一直一样吗?cProofs0001.pdfartcProofs0001.pdf3S

评论

0赞 cnjfo672 11/3/2022
效果很好!正是我需要完成这项任务。谢谢!是的,顺序将始终相同。