Convert file from Windows to UNIX through Powershell or Batch

Question:

I have a batch script that prompts a user for some input then outputs a couple of files I’m using in an AIX environment. These files need to be in UNIX format (which I believe is UTF8), but I’m looking for some direction on the SIMPLEST way of doing this.

I don’t like to have to download extra software packages; Cygwin or GnuWin32. I don’t mind coding this if it is possible, my coding options are Batch, Powershell and VBS. Does anyone know of a way to do this?

Alternatively could I create the files with Batch and call a Powershell script to reform these?

The idea here is a user would be prompted for some information, then I output a standard file which are basically prompt answers in AIX for a job. I’m using Batch initially, because I didn’t know that I would run into this problem, but I’m kind of leaning towards redoing this in Powershell. because I had found some code on another forum that can do the conversion (below).

Looking for some direction or some input on this.

Answer:

You can’t do this without external tools in batch files.

If all you need is the file encoding, then the snippet you gave should work. If you want to convert the files inline (instead of writing them to another place) you can do

(the parentheses around Get-Content are important) However, this will write the files in UTF-8 with a signature at the start (U+FEFF) which some Unix tools don’t accept (even though it’s technically legal, though discouraged to use).

Then there is the problem that line breaks are different between Windows and Unix. Unix uses only U+000A (LF) while Windows uses two characters for that: U+000D U+000A (CR+LF). So ideally you’d convert the line breaks, too. But that gets a little more complex:

Source:

Convert file from Windows to UNIX through Powershell or Batch by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply