-1

I have a ASCII file generated by Visual Studio that has inconsistent line endings and there is some character values that are > 127 in it.

I'd like to read in the file, perform a regex replace on the text (ignoring character values > 127) and then write the file back without changing the line endings or the characters of a value > 127.

The best that I have is:

(Get-Content $rcFile) -replace 
     "(FILEVERSION\s+|VALUE\s+`"(?:FileVersion|ProductVersion)`",\s+(`"))$old_major([,.])$old_minor2(?:\3)$old_minor1(?:\3)0",
     "`${1}$new_major`${3}$new_minor2`${3}$new_minor1`${3}0" | Set-Content "$rcfile.new"

But the line endings are not as they were. There's a stray \r near the beginning and near the end that get converted to a \r\n with this. I'd like to keep this as it was as it keeps being kicked back in by the VS environment and it causes the file to be checked out for no reason.

Is this a lost cause?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Adrian
  • 10,246
  • 4
  • 44
  • 110
  • Try [no Formatting](http://stackoverflow.com/questions/5019403/editing-xml-with-powershell-and-file-format-error) – lloyd Mar 02 '16 at 00:50
  • @lloyd, that link is for an XML file. The file that this is happening on is a plain text file. – Adrian Mar 02 '16 at 01:07
  • Then a [casting issue](http://stackoverflow.com/questions/15041857/powershell-keep-text-formatting-when-reading-in-a-file). Perhaps include more than a partial line of powershell so I can tell what is occurring. – lloyd Mar 02 '16 at 08:09

1 Answers1

1

Starting with PowerShell v3 Get-Content has a parameter -Raw to read an entire file without splitting it into an array of lines. However, Set-Content will always put CR-LF at the end of a string it writes, so that approach won't work for you.

Use the ReadAllText() and WriteAllText() methods instead:

$txt = [IO.File]::ReadAllText($rcFile) -replace ...
[IO.File]::WriteAllText("$rcFile.new", $txt)

If needed you can specify the encoding of the file read and/or the file to be written:

$txt = [IO.File]::ReadAllText($rcFile, $readEncoding) -replace ...
[IO.File]::WriteAllText("$rcFile.new", $txt, $writeEncoding)

Some encodings are available as pre-defined constants (e.g. [Text.Encoding]::ASCII or [Text.Encoding]::UTF8), but you can also define encodings by codepage. In this case the file was apparently encoded using the US OEM codepage ([Text.Encoding]::GetEncoding('IBM437')).

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Yeah, I've tried that already. That's why I stated `character values that are > 127` as this will mess up those characters. – Adrian Mar 02 '16 at 18:36
  • Without seeing the actual content of the file the only other things I could suggest are to play with the encoding, or to edit the bytes of the file (`ReadAllBytes()`/`WriteAllBytes()`). – Ansgar Wiechers Mar 02 '16 at 22:25
  • The file is an `.rc` file and the contents is mostly regular text with mostly `\r\n` line endings. There are some `\r` line endings, and some strings that consist of characters of hex values `92`, `96`, `A3`, `A9`, and others. I'll try and play around with the `encoding` parameter. – Adrian Mar 02 '16 at 22:52
  • I'm not sure how to generate a new `[System.Text]::ASCIIEncoding` object in powershell. Do you have an example? – Adrian Mar 02 '16 at 23:01
  • `[Text.Encoding]::ASCII` or `[Text.Encoding]::GetEncoding($codepage)`. [Documentation](https://msdn.microsoft.com/en-us/library/system.text.encoding%28v=vs.90%29.aspx). – Ansgar Wiechers Mar 02 '16 at 23:05
  • Yeah, already got that, but it says that it's unable to find type. Can you post and example? I've tried `Add-Type -AssemblyName System.Text` but it complains that `Add-Type : Cannot add type. The assembly 'System.Text' could not be found.` – Adrian Mar 02 '16 at 23:22
  • Ahh, nm. Got it using `Add-Type -AssemblyName System.Text.Encoding`. ASCII didn't work, I'll try some others. – Adrian Mar 02 '16 at 23:34
  • That was it. I used `[System.Text.Encoding]::GetEncoding('IBM437')` as my encoding type and it worked like a charm. Update your answer to include switching encoding types with example(s) and I'll mark it as answered. – Adrian Mar 02 '16 at 23:43
  • Adjusted answer. Note that the `System.Text.Encoding` namespace is available by default in PowerShell. No `Add-Type` required. – Ansgar Wiechers Mar 03 '16 at 08:46
  • Ahh. Yeah, Must have been using the wrong type path. Thanks. :) – Adrian Mar 03 '16 at 14:08