2

Background

I have a pipe delimited csv file that looks like this:

ColA|ColB|ColC|ColD|ColE|ColF|ColG|ColH|ColI|ColJ|ColK
00000000|000000507|0000000|STATUS|0000|000000|000|0000|00|0000|00000
00000000|000000500|0000000|STATUS|0000|000000|000|0000|00|0000|00000
00000000|000007077|0000000|STATUS|0000|000000|000|0000|00|0000|00000

I want to take ColB on lines with a certain status and put it in a headless csv file like this:

000000507,0000000001,0,0
000000500,0000000001,0,0
000007077,0000000001,0,0

The values 0000000001,0,0 on the end of the line are identical for every item.

The Script

The trimmed down/generic version of the script looks like this:

$infile = Import-Csv ".\incsv.txt" -Delimiter '|'
$outfile = ".\outcsv.txt"

Foreach($inline in $infile){
  If($inline.Status -ne 'Some Status'){
    $outline = $inline.'ColB' + ',0000000001,0,0'
    Add-Content $outfile $outline -Encoding ASCII
  }
}

The Problem

The problem is that the new file that is created is about twice the size it should be, and I know it has to do with the encoding. Unfortunately, I can't find an encoding that works. I've tried -Encoding ASCII, -Encoding Default, -Encoding UTF8, but they all are too large.

This wouldn't be an issue, but the program that reads the created text file won't read it correctly.

What I can do, is copy the text from the new file in Notepad, save it as ANSI, and it works fine. ANSI isn't an option in the -Encoding parameter though.

How do I get Powershell to output the correct file type? Is there a better way to approach this?

I've found this Google Groups conversation and this Social TechNet post, but neither one actually worked for me.

0

1 Answer 1

2

If the output file already exists and is in Unicode format the parameter -Encoding ASCII is ignored when appending to the file. Change your loop to this:

$infile | % {
  if ($_.Status -ne 'Some Status') {
    $_.'ColB' + ',0000000001,0,0'
  }
} | Set-Content $outfile -Encoding ASCII
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.