1

Am copying file from remote server and saving in my local machine using PowerShell. But after I copied filed from remote server while I open that file in Notepad, it's not opening in proper format (alignment). But if I copying using FTP command manually its alignment is proper in Notepad.

Please find my PowerShell script:

$File = "D:\copiedfile.txt"
$ftp = "ftp://remote_machine_name//tmp/text.txt"
$ftprequest = [System.Net.FtpWebRequest]::Create($ftp)
$ftprequest.UseBinary = $true

"ftp url: $ftp"
$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)
"Downloading $File..."
$webclient.DownloadFile($uri, $File)

Please find attached screenshot after copying file using PowerShell script (not in proper alignment).

enter image description here

Please find attached screenshot after copying file using FTP manually (proper alignment).

enter image description here

Am getting this alignment issue due to the cross-platform. Am copying file from HP-UX to Windows. Not sure, how to solve this problem.

While am copying file via FTP manually (commandline) its transfer mode is ASCII. But am not sure how to set transfer mode for ASCII in my powershell script.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Karthikeyan
  • 1,927
  • 6
  • 44
  • 109

1 Answers1

1

Windows notepad supports only Windows EOL. Your file has most probably *nix EOL.

You need to use ascii/text mode, not binary, so that FtpWebRequest can convert the file to Windows EOL.

$ftprequest.UseBinary = $False

Though note that while you create the FtpWebRequest, you never really use it.

A full code is like:

$url = "ftp://remote_machine_name//tmp/text.txt"
$ftprequest = [System.Net.FtpWebRequest]::Create($url)
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$ftprequest.UseBinary = $false

$ftpresponse = $ftprequest.GetResponse()
$responsestream = $ftpresponse.GetResponseStream()

$localPath = "D:\copiedfile.txt"
$targetfile = New-Object IO.FileStream($localPath, [IO.FileMode]::Create)
$responsestream.CopyTo($targetfile);
$responsestream.Close()
$targetfile.Close()

(and remove all the WebClient code)

Stream.CopyTo was added in .NET 4. If you need to use an older version of .NET, you need to copy stream contents in a loop, as shown for example in Changing FTP from binary to ascii in PowerShell script using WebClient.


It works correctly with command-line ftp, as it defaults to the ascii/text mode.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • i tried with `$ftprequest.UseBinary = $False` . But still am getting the file which is alignment is not proper in Notepad – Karthikeyan Mar 26 '18 at 14:14
  • OK, I see. Note that while you create the `FtpWebRequest`, you never really use it. See my updated answer. – Martin Prikryl Mar 26 '18 at 14:20
  • Now am getting this error `Method invocation failed because [System.Net.FtpDataStream] doesn't contain a method name 'CopyTo'` – Karthikeyan Mar 26 '18 at 14:24
  • 1
    `CopyTo` was added in .NET 4. If you need to use an older version of .NET, you need to copy stream contents in a loop, as shown for example here: https://stackoverflow.com/q/30946824/850848 – Martin Prikryl Mar 26 '18 at 14:28