Can set which file type is considered ASCII via script?

Advertisement

marcusvdt
Joined:
Posts:
4

Can set which file type is considered ASCII via script?

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:
$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.

Reply with quote

Advertisement

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!

Reply with quote

marcusvdt
Joined:
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.

Reply with quote

marcusvdt
Joined:
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.

$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

Reply with quote

Advertisement

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

Good job!

but since I don't have Winscp installed on the Windows server
WinSCP has to be there. The assembly does not work without it.

Reply with quote

marcusvdt
Joined:
Posts:
4

martin wrote:

Good job!

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?

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,442
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.

Reply with quote

Advertisement

You can post new topics in this forum