File Lf to CrLf conversion using PowerShell

R

Randy

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
 

My Computer

R

Randy

Correction: It was SED, not PERL.

"Randy" wrote:

> 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
>
 

My Computer

T

tojo2000

On May 19, 2:01 pm, Randy <[email protected]> wrote:

> 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 Iam
> willing to start.  
>
> Thanks,
> Randy
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
 

My Computer

R

Randy

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%
)


"tojo2000" wrote:

> On May 19, 2:01 pm, Randy <[email protected]> wrote:

> > 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
>
> 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
>
>
 

My Computer

J

Joel Bennett

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


Randy wrote:

> 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
>
 

My Computer

Top