Link to home
Start Free TrialLog in
Avatar of First Last
First LastFlag for United States of America

asked on

WinSCP scripting novice needs help with ASCII file transfer

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 in a text editor 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
 }
Avatar of AlexPace
AlexPace
Flag of United States of America image

Not all SFTP connections can do ASCII mode.  The original SFTP had no ASCII mode at all, it was all binary.  Some FTP/SFTP client software are able to "fake it" by replacing \r or \n with \r\n.  The newer SFTP spec has a text mode that should do this automatically but both the client and server must support the protocol extensions.

I don't know if WinSCP supports the newer extensions or if it can fake it if the server doesn't support it.  I think Robo-FTP can fake it... a Robo-FTP script that does what your script does would look something like this:
WORKINGDIR "C:\XXXX File\"
FTPLOGON "XX.XXX.net" /user="USERNAME" /pw="PASSWORD" /servertype=SFTP /trust=all
FTPCD "/Inbox"
RCVFILE "file.txt" /type=ASCII
FTPLOGOFF

Open in new window

If you saved that as a file named download.s then you could launch it from a command line like this:
   robo-ftp.exe -s"download.s"

... you can launch so it is invisible by also adding the -v command line argument
Avatar of Gerwin Jansen
Did you try changing the protcol FTP?

$sessionOptions.Protocol = [WinSCP.Protocol]::Ftp
Avatar of First Last

ASKER

@AlexPace - Do you know if robo-ftp supports the sftp protocol?  That is the only type that they will allow.  The version of WinSCP I'm running (current non-beta) does support both ASCII and Binary but I'm not 100% sure my syntax is right.

@gerwinjansen - the company I am transferring the file from requires a sftp connection unfortunately.
Then you should use binary transfer and make sure that your source file contains CRLF.
ASKER CERTIFIED SOLUTION
Avatar of AlexPace
AlexPace
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial