Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

File Lf to CrLf conversion using PowerShell

1,017 views
Skip to first unread message

Randy

unread,
May 19, 2009, 5:01:02 PM5/19/09
to
Hi,

We are upgrading a process from FTP/PGP to SFTP (PUTTY/PSCP). The FTP
handles the Lf to CrLf conversion when in ASCII mode. However, for now,
there is only a BINARY mode with SFTP. I looked online and noticed several
"one-liners" in PERL to do this conversion. In PowerShell, how can I create
a file that has the correct CrLf terminator? I would likely use it in a SQL
Server 2005 job or SSIS package. I have not used PowerShell yet, but I am
willing to start.

Thanks,
Randy

Randy

unread,
May 19, 2009, 6:55:02 PM5/19/09
to
Correction: It was SED, not PERL.

tojo2000

unread,
May 19, 2009, 9:29:09 PM5/19/09
to

PowerShell will detect LF as a newline when reading it, but doesn't
convert it implicitly.

Something like this should work:

(gc infile.txt) | %{$_.split("`n")} | Out-File outfile.txt

Randy

unread,
May 19, 2009, 10:02:12 PM5/19/09
to
Thanks. BTW, I did it in batch, but.... I look forward to the new way.

FOR /F "eol= delims= usebackq" %%a IN (%TARGET_RAW%) DO (
ECHO %%a>> %TARGET%
)

Joel Bennett

unread,
May 22, 2009, 9:18:27 AM5/22/09
to
When READING, powershell will handle either `n or `r or `r`n ... so
Get-Content $file .... will "just work."

Set-Content always uses [Environment]::Newline, which basically means
that it generally uses "`r`n" ... to work around that, you need to set
it yourself.

To convert an "OldFile.txt" from (whatever) to [Environment]::Newline,
you could do:

Get-Content OldFile.txt | Set-Content NewFile.txt


To convert it and specify the line-terminator character:

Set-Content newfile.txt $(
[string]::Join( "`n", (Get-Content OldFile.txt))

--
Joel

0 new messages