提取 xml 标签时将文件名附加到输出
Append filename to output when extracting an xml tag
我制作了一个脚本,可以从多个 xml 文件中提取 iban 标签,结果输出到一个 csv 文件。但是,我在获取文件名和 iban 值时遇到了问题。当我将它写入 screen 时,它位于 $item 变量中,当我试图在输出中连接这两个变量时,我不允许这样做。有人给我提示吗?
Printscreen of running script
$items = Get-ChildItem "C:\temp\cif01_rda_camt054c\*.xml" | Select-Object -ExpandProperty Fullname
#Loop over them and append them to the document
foreach ($item in $items) {
[xml]$XmlDocument = Get-Content $item
$IBAN = $XmlDocument.Document.BkToCstmrDbtCdtNtfctn.Ntfctn.Acct.Id
write $IBAN
write $item
write $filename
$IBAN | Export-Csv -path C:\temp\cif01_rda_camt054c\myoutput.csv -NoTypeInformation -Append
}
output after suggested changes
您需要构建一个 对象,它将对应于生成的 CSV 文件中的每条记录:
$items = Get-ChildItem "C:\temp\cif01_rda_camt054c\*.xml" | Select-Object -ExpandProperty Fullname
#Loop over them and append them to the document
foreach ($item in $items) {
[xml]$XmlDocument = Get-Content $item
$IBAN = $XmlDocument.Document.BkToCstmrDbtCdtNtfctn.Ntfctn.Acct.Id
# Create an object with an IBAN property and a FilePath property
# The resulting CSV file will have the column headers "IBAN" and "FilePath"
[pscustomobject]@{
IBAN = $IBAN
FilePath = $item
} | Export-Csv -path C:\temp\cif01_rda_camt054c\myoutput.csv -NoTypeInformation -Append
}
更新脚本以备不时之需。
$items = Get-ChildItem "C:\temp\cif01_rda_camt054c\*.xml" | Select-Object -ExpandProperty 全名
#遍历它们并将它们附加到文档中
foreach ($item in $items) {
[xml]$XmlDocument = Get-Content $item
#specify relevant xmltag below
$IBAN = $XmlDocument.Document.BkToCstmrDbtCdtNtfctn.Ntfctn.Acct.Id
#write $iban
# Create an object with an IBAN property and a FilePath property
# The resulting CSV file will have the column headers "IBAN" and "FilePath"
[pscustomobject]@{
IBAN = $IBAN.IBAN
FilePath = $item
} | Export-Csv -path C:\temp\cif01_rda_camt054c\myoutput.csv -NoTypeInformation -Append
}
我制作了一个脚本,可以从多个 xml 文件中提取 iban 标签,结果输出到一个 csv 文件。但是,我在获取文件名和 iban 值时遇到了问题。当我将它写入 screen 时,它位于 $item 变量中,当我试图在输出中连接这两个变量时,我不允许这样做。有人给我提示吗?
Printscreen of running script
$items = Get-ChildItem "C:\temp\cif01_rda_camt054c\*.xml" | Select-Object -ExpandProperty Fullname
#Loop over them and append them to the document
foreach ($item in $items) {
[xml]$XmlDocument = Get-Content $item
$IBAN = $XmlDocument.Document.BkToCstmrDbtCdtNtfctn.Ntfctn.Acct.Id
write $IBAN
write $item
write $filename
$IBAN | Export-Csv -path C:\temp\cif01_rda_camt054c\myoutput.csv -NoTypeInformation -Append
}
output after suggested changes
您需要构建一个 对象,它将对应于生成的 CSV 文件中的每条记录:
$items = Get-ChildItem "C:\temp\cif01_rda_camt054c\*.xml" | Select-Object -ExpandProperty Fullname
#Loop over them and append them to the document
foreach ($item in $items) {
[xml]$XmlDocument = Get-Content $item
$IBAN = $XmlDocument.Document.BkToCstmrDbtCdtNtfctn.Ntfctn.Acct.Id
# Create an object with an IBAN property and a FilePath property
# The resulting CSV file will have the column headers "IBAN" and "FilePath"
[pscustomobject]@{
IBAN = $IBAN
FilePath = $item
} | Export-Csv -path C:\temp\cif01_rda_camt054c\myoutput.csv -NoTypeInformation -Append
}
更新脚本以备不时之需。
$items = Get-ChildItem "C:\temp\cif01_rda_camt054c\*.xml" | Select-Object -ExpandProperty 全名
#遍历它们并将它们附加到文档中 foreach ($item in $items) {
[xml]$XmlDocument = Get-Content $item
#specify relevant xmltag below
$IBAN = $XmlDocument.Document.BkToCstmrDbtCdtNtfctn.Ntfctn.Acct.Id
#write $iban
# Create an object with an IBAN property and a FilePath property
# The resulting CSV file will have the column headers "IBAN" and "FilePath"
[pscustomobject]@{
IBAN = $IBAN.IBAN
FilePath = $item
} | Export-Csv -path C:\temp\cif01_rda_camt054c\myoutput.csv -NoTypeInformation -Append
}