Topic "WinSCP novice needs help with ASCII/Binary transfer script"

Author Message
danbrown1888
[View user's profile]

Joined: 2013-05-31
Posts: 4
Location: NJ
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
}
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
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.
danbrown1888
[View user's profile]

Joined: 2013-05-31
Posts: 4
Location: NJ
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.
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
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:
http://winscp.net/eng/docs/rawsettings
_________________
Martin Prikryl
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License