Topic "Can set which file type is considered ASCII via script?"

Author Message
marcusvdt
[View user's profile]

Joined: 2013-10-21
Posts: 4
I developed a powershell (winscp .net) script to synchronize a directory, downloading all the files from a remote Linux machine to a local Windows server through SFTP. In the local machine, these files are used in different ways, most of them are read by corporate softwares.
This is the relevant part of the script which deals with the sync:
Code:

$global:sessionOptions = New-Object WinSCP.SessionOptions
$global:sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$global:sessionOptions.HostName = $customservername
$global:sessionOptions.UserName = $customsftpuser
$global:sessionOptions.SshPrivateKeyPath = $ppkfile
$global:sessionOptions.SshHostKeyFingerprint = $rsakey

$global:session = New-Object WinSCP.Session
$global:session.Open($global:sessionOptions)
$global:transferOptions = New-Object WinSCP.TransferOptions
$global:transferOptions.TransferMode = [WinSCP.TransferMode]::Automatic
$synchronizationResult = $global:session.SynchronizeDirectories([WinSCP.SynchronizationMode]::Local,$localdirectorypath, $remotepath, $False, $False, [WinSCP.SynchronizationCriteria]::Time, $global:transferOptions)

bla bla bla



It is working perfectly for 99.99% of the files it deals it, but recently the user which is monitoring this script on the server told me there is a problem with line breaks with one of the file types.

My script is set to use automatic file type handling and it is working for known file types, like pdf, xml and txt files. The problem is that there is a custom file type *.LBL which in fact is a text file that is generated on the remote Linux which is being downloaded in binary mode to the local Windows server. I suppose the automatic transfer mode is considering this file type as unknown and hence downloading them in binary mode.

In the Windows server, there is a hardware attached to the machine which reads these *.LBL files and it expects for CRLF line breaks, not LF only. So I can't change the file extension on the source and I don't believe it is a good approach to deal with *.LBL files separately. The required solution is to keep the script almost as is, applying the least changes as possible, but still making it able to download these *.LBL files as ASCII so the line breaks are properly converted during the synchronization.

I'm trying to avoid making significant modifications on the way the current script works because it is used to sync many file types and it is working perfectly to the other files. So I'm wondering if it is possible to change what file extensions are considered as ASCII files in the automatic transfer mode, but need to change that setting through script.
In other words, I'm looking for help on changing the settings through script, so the *.LBL file extension is considered as "text" and hence it would be downloaded in ASCII mode (when using automatic transfer mode like in the code above).

PS: the server doesn't have Winscp installed.

Any help is very appreciated. This issue is kind of urgent now.
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24991
Location: Prague, Czechia
The .NET assembly does not support this directly.

You would have to break the configuration isolation from GUI WinSCP (see Session.DefaultConfiguration) and configure the ASCII file mask in the GUI.
Guest




I'll do like you said with Session.DefaultConfiguration but since I don't have Winscp installed on the Windows server, I'm wondering if it would work if I create only the specific registry keys that deal with this setting on my Windows server.
I'll probably finish testing this before you reply, but anyway...

I'll report back my results.

Thank you very much!
marcusvdt
[View user's profile]

Joined: 2013-10-21
Posts: 4
Anonymous wrote:
I'll do like you said with Session.DefaultConfiguration but since I don't have Winscp installed on the Windows server, I'm wondering if it would work if I create only the specific registry keys that deal with this setting on my Windows server.
I'll probably finish testing this before you reply, but anyway...

I'll report back my results.

Thank you very much!

It was me on another computer.
marcusvdt
[View user's profile]

Joined: 2013-10-21
Posts: 4
Good news! It worked, but with a winscp.ini file instead of a registry setting.

I used session.DefaultConfiguration = $False and created a standard winscp.ini file in the same folder as winscp.exe but have modified it to include the *.lbl extension on the ASCII extensions.

Code:

$global:sessionOptions = New-Object WinSCP.SessionOptions
$global:sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$global:sessionOptions.HostName = $customservername
$global:sessionOptions.UserName = $customsftpuser
$global:sessionOptions.SshPrivateKeyPath = $ppkfile
$global:sessionOptions.SshHostKeyFingerprint = $rsakey

$global:session = New-Object WinSCP.Session
$global:session.DefaultConfiguration = $False
$global:session.Open($global:sessionOptions)
$global:transferOptions = New-Object WinSCP.TransferOptions
$global:transferOptions.TransferMode = [WinSCP.TransferMode]::Automatic
$synchronizationResult = $global:session.SynchronizeDirectories([WinSCP.SynchronizationMode]::Local,$localdirectorypath, $remotepath, $False, $False, [WinSCP.SynchronizationCriteria]::Time, $global:transferOptions)

bla bla bla
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24991
Location: Prague, Czechia
Good job!

Quote:
but since I don't have Winscp installed on the Windows server

WinSCP has to be there. The assembly does not work without it.
_________________
Martin Prikryl
marcusvdt
[View user's profile]

Joined: 2013-10-21
Posts: 4
prikryl wrote:
Good job!

Quote:
but since I don't have Winscp installed on the Windows server

WinSCP has to be there. The assembly does not work without it.


As far as I know winscp is not really installed on that server. But I've included the WinSCP.exe portable in the same directory as the script itself, together with winscp.dll, winscp.com and the new winscp.ini that I've created.

Would this work without WinSCP installed on the server?
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24991
Location: Prague, Czechia
marcusvdt wrote:
As far as I know winscp is not really installed on that server. But I've included the WinSCP.exe portable in the same directory as the script itself, together with winscp.dll, winscp.com and the new winscp.ini that I've created.

Would this work without WinSCP installed on the server?

Sure. No "installation" is ever needed for WinSCP. But you need to have the .exe there.
_________________
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