将列表中的一行添加到第二个列表 Powershell 中的一行
Adding a row from a List to a row in a 2nd List Powershell
在 Powershell 中:我有 2 个列表——FontIDList 和 FontDefinitionList。我想获取 FontID 的第一行并添加 FontDefinition 的第一行。重复所有元素,然后写入文本文件。我试过:
$newContent = $FontIDList2 | foreach {$_ + $FontDefList2}
Add-Content "C:\Desktop\MasterFontList-TimeStamps\TestLatestDate.txt" -value $newContent
以及 $newContent = $FontIDList2 + FontDefList2
-我很确定我需要使用某种 foreach
,但不确定语法。
您可以通过索引而不是使用 for
循环进行迭代,然后连接您的字符串:
$newContent = for ($i = 0 ; $i -lt $FontIDList.Count ; $i++) {
$FontIDList[$i] + $FontDefList2[$i]
}
Add-Content "C:\Users\dtxxxxx\Desktop\MasterFontList-TimeStamps\TestLatestDate.txt" -value $newContent
请注意 Add-Content
添加到文件中,不确定这是否是您真正想要的。
使用 Linq 的 Zip 方法:
$selector = [func[object, object, object]] {
param($x1, $x2)
$x1 + $x2
}
[Linq.Enumerable]::Zip($FontIDList2, $FontDefList2, $selector) | Add-Content out.txt
在 Powershell 中:我有 2 个列表——FontIDList 和 FontDefinitionList。我想获取 FontID 的第一行并添加 FontDefinition 的第一行。重复所有元素,然后写入文本文件。我试过:
$newContent = $FontIDList2 | foreach {$_ + $FontDefList2}
Add-Content "C:\Desktop\MasterFontList-TimeStamps\TestLatestDate.txt" -value $newContent
以及 $newContent = $FontIDList2 + FontDefList2
-我很确定我需要使用某种 foreach
,但不确定语法。
您可以通过索引而不是使用 for
循环进行迭代,然后连接您的字符串:
$newContent = for ($i = 0 ; $i -lt $FontIDList.Count ; $i++) {
$FontIDList[$i] + $FontDefList2[$i]
}
Add-Content "C:\Users\dtxxxxx\Desktop\MasterFontList-TimeStamps\TestLatestDate.txt" -value $newContent
请注意 Add-Content
添加到文件中,不确定这是否是您真正想要的。
使用 Linq 的 Zip 方法:
$selector = [func[object, object, object]] {
param($x1, $x2)
$x1 + $x2
}
[Linq.Enumerable]::Zip($FontIDList2, $FontDefList2, $selector) | Add-Content out.txt