WinSCP novice needs help with ASCII/Binary transfer script

Advertisement

danbrown1888
Joined:
Posts:
4
Location:
NJ

WinSCP novice needs help with ASCII/Binary transfer script

Hello scripting experts! I'm a network admin attempting to use WinSCP scripting for a file transfer. The problem I am encountering is the format. The file needs to be in standard ASCII with CRLF at the end of each line. Right now when viewed those are missing. I have made several attempts at changing the script parameters but have seen no change which leads me to believe the problem is my syntax. The script is below, I would greatly appreciate any feedback on how to modify it to force the file to transfer in ASCII format so I get the CRLF formatting to show up. Thanks in advance!

try
{
# Load WinSCP .NET assembly
# Use "winscp.dll" for the releases before the latest beta version.
[Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\WinSCP\WinSCP.dll") | Out-Null

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "XX.XXX.net"
$sessionOptions.UserName = "USERNAME"
$sessionOptions.Password = "PASSWORD"
$sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"

$session = New-Object WinSCP.Session

try
{
# Connect
$session.Open($sessionOptions)

$localPath = "C:\XXXX File\"
$remotePath = "/Inbox/"
$file = "file.txt"

# Transfer Options set to ASCII
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::ASCII

# Download the file and throw on any error
$session.GetFiles(
($remotePath + $file),
($localPath + $file)).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}

exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,453
Location:
Prague, Czechia

Re: WinSCP novice needs help with ASCII/Binary transfer script

If you need the file to be CRLF at the server-end, just upload it in binary format, what is is default. (I suppose you have it in CRLF in source as you are on Windows).

Otherwise your code to enable Ascii mode is correct. Problem is that files are converted to LF line-endings with SFTP (version 3 and older) by default.

Reply with quote

danbrown1888

I've tried changing the format using ASCII, Binary, and default but the CRLF I need at the end of each line still never shows up. I thought maybe I had a syntax issue in my script which was preventing the transfer type from being correctly set.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,453
Location:
Prague, Czechia

First note that file transfer client is not generic line ending conversion tool. So generally, it only converts the line ending between native formats of source and destination machine. And you do not seem to have the source file in native format of your local machine (Windows).

Anyway, assuming your server supports SFTP version 3, not higher, what is common, you can force WinSCP to convert the file to CRLF using raw session setting EOLType:
https://winscp.net/eng/docs/rawsettings

Reply with quote

Advertisement

You can post new topics in this forum