パイプで区切られたファイルをループするときに、その行に28列あり、その行をファイルにエクスポートするだけです。これは私が探している主な質問と答えです(私は多くのことを研究しましたが、私はPSには新しく、いくつかの助けが必要です)。以下の作品は2つの問題があります。エクスポートファイルでは、区切られたパイプを選択することはできません。出力は、序数ではなく、フィールド名でアルファベット順に表示されます。Powershellループcsvファイルcsv行を1つずつエクスポートする
また、出力を「テキスト」に修飾しない方法もありますか?
$path = Get-ChildItem c:\temp\*.txt
$staticPath = '0859'
$year = Get-Date -format yy
$month = Get-Date -format MM
$day = Get-Date -format dd
$output = 'c:\temp\' + $year + $month + $day + $staticPath + '.TXT'
$outputbad = 'c:\temp\BAD' + $year + $month + $day + $staticPath + '.TXT'
$input = 'c:\temp\' + $path.Name
$input
$csv = Import-Csv -path $input -Delimiter "|"
foreach($line in $csv)
{
$properties = $line | Get-Member -MemberType Properties
$row = ''
$properties.Count
$obj = new-object PSObject
for($i=0; $i -lt $properties.Count;$i++)
{
$column = $properties[$i]
$columnvalue = $line | Select -ExpandProperty $column.Name
#$row += $columnvalue
$obj | add-member -membertype NoteProperty -name $column.Name -value $columnvalue
}
if($properties.Count -eq 28)
{
$obj | export-csv -Path $output -Append -NoTypeInformation
#$row | Export-Csv -Path $output -Append -Delimiter "|" -NoTypeInformation
}
else
{
$obj | Export-Csv -Path $outputbad -Append -Delimiter "|" -NoTypeInformation
}
}
'Import-Csv'がCSVテキストの行を' PSObject'インスタンスに「水和」すると、CSVのフィールドの順序は失われます。 'PSObject'インスタンス(' $ line')と 'Get-Member'コマンドレットのどちらもフィールドの元の順序を知る方法はありません。 – BACON