WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: MW4 on April 17, 2014, 10:39:16 AM

Title: Suggestions for an SFTP process
Post by: MW4 on April 17, 2014, 10:39:16 AM
I have a process that I need to set up that will do the following:
  1.  Connect with an SFTP server
  2.  Download some specifically named files to a local folder
  3.  Decrypt all of the files in that folder.
  4.  Them move them to the specific folders that they will need to reside in for use.

We have used Cleopatra on a singular file basis and the connect and decrypt works fine, we just need to automate it into a scheduler job.
Any help in pointing me in the right direction would be fantastic.
If there is a better solution than Cleopatra, I'm all ears.
Title: Re: Suggestions for an SFTP process
Post by: Deana on April 17, 2014, 10:47:10 AM
You're in luck! I recently wrote somecode to handle this and posted it to the tech database: http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WinBatch/dotNet/Third~Party~Components/WinScp+SFTP~using~WinSCP~dotNet~.txt
Title: Re: Suggestions for an SFTP process
Post by: MW4 on April 24, 2014, 03:21:44 PM
ok, I hit a snag with the script.

ObjectClrOption ( 'use', 'WinSCP, Version=1.1.5.4214, Culture=neutral' )

that is the version of the dot.net dll that I downloaded today


Title: Re: Suggestions for an SFTP process
Post by: Deana on April 24, 2014, 04:55:54 PM
You might want to make sure your are using the correct assembly directory path in the APPBASE statement, before calling the USE. Also click on the More Error Info button and post the resulting extended error information.
Title: Re: Suggestions for an SFTP process
Post by: MW4 on June 26, 2014, 12:39:42 PM
More error info
Title: Re: Suggestions for an SFTP process
Post by: MW4 on June 26, 2014, 12:45:44 PM
Currently I can pull the files manually until I can get the WinSCP part working.

So...

How about the Decrypt part, what is suggested for that?
Title: Re: Suggestions for an SFTP process
Post by: Deana on June 26, 2014, 12:52:09 PM
Did you check the AppBase folder is correctly pointing the WinSCP directory?

What is the name of the dll in the WinSCP directory?



Title: Re: Suggestions for an SFTP process
Post by: MW4 on June 26, 2014, 02:06:07 PM
Yes, pointing correctly
Title: Re: Suggestions for an SFTP process
Post by: Deana on June 26, 2014, 02:46:01 PM
The dll name is WinSCPnet.dll therefore your script should read:

Code (winbatch) Select
ObjectClrOption ( 'use', 'WinSCPnet, Version=1.1.5.4214, Culture=neutral' )
Title: Re: Suggestions for an SFTP process
Post by: richardh on July 01, 2014, 05:12:18 AM
I will definitely be using this example for my needs.
Thanks Deana  :)
Title: Re: Suggestions for an SFTP process
Post by: MW4 on July 11, 2014, 12:33:16 PM
Getting these two errors in the attachments...
looks like WinSCPnet is not coming through (from trace below)

ObjectClrOption ( 'use', 'WinSCPnet, Version=1.1.5.4214, Culture=neutral' )

mscorlib=Could not load file or assembly 'WinSCP, Version=1.1.5.4214, Culture=neutral' or one of its dependencies. The module was expected to contain an assembly manifest.

Title: Re: Suggestions for an SFTP process
Post by: td on July 11, 2014, 01:08:03 PM
The "mscorlib=Could..." error is just a legacy error living on in the ini file.  The second error is relevant and indicates that you are either using an invalid or incorrectly formated thumb print. You need to verify that you have the correct hash and that it is in the format expected by the SshHostKeyFingerprint method.  Also, you need to check that all the other parameter option you are assigning are correct as any of these could might be the source of the error.
Title: Re: Suggestions for an SFTP process
Post by: MW4 on July 11, 2014, 01:20:44 PM
I had mis matched Program versions and the .net dll
Fixed that.
I also had a bad path...fixed and works!!!!!


Woo HOO!!!

Thank You  Deana!!

Title: Re: Suggestions for an SFTP process
Post by: MW4 on July 11, 2014, 01:37:05 PM
Now as a finishing piece...

what's the command to delete that file from the FTP server after a download?


second finishing piece....

Files are in that folder as this:
/outbound/NNA2FAIR_INVOC.2014_07_10_0132.TXT.PGP
/outbound/NNA2FAIR_INVOC.2014_07_10_0145.TXT.PGP
/outbound/NNA2FAIR_INVOC.2014_07_10_0156.TXT.PGP
/outbound/NNA2FAIR_INVOC.2014_07_10_0159.TXT.PGP

I need to pull many files that start with /outbound/NNA2FAIR_INVOC <WILDCARD>
and save them to my local dir using the whole filename.

The folder contains files that are not mine so I don't want to sync the entire directory.

Using the code like it is, how would you accomplish that?


Title: Re: Suggestions for an SFTP process
Post by: td on July 11, 2014, 02:02:05 PM
The online documentation for the WinSCP assembly is quit good.  I recommend you check it out.  It clearly states that the 'GetFiles' method accepts wildcards so if your files can be identified by name, you could construct a wildcard file name the matches the files you want to retrieve.
Title: Re: Suggestions for an SFTP process
Post by: MW4 on July 11, 2014, 02:10:12 PM
I just looked through that and it works quite well.

I got the wildcard and am about to test the delete.

Thanks again for the help
Title: Re: Suggestions for an SFTP process
Post by: MW4 on July 11, 2014, 03:29:29 PM
bFalse = ObjectType( 'BOOL', 1 )

1 sets delete



transferResult = objSession.GetFiles( '/outbound/NNA2FAIR*', 'c:\tempy\', bFalse, objTransferOptions) 

* = standard wildcard
Title: Re: Suggestions for an SFTP process
Post by: MW4 on September 22, 2014, 10:18:30 AM
One further question on this topic...

What is the easiest way to query up the "oldest" file and then just download that one... IE:

from these files in the directory
/outbound/NNA2FAIR_INVOC.2014_07_10_0132.TXT.PGP
/outbound/NNA2FAIR_INVOC.2014_07_10_0145.TXT.PGP
/outbound/NNA2FAIR_INVOC.2014_07_10_0156.TXT.PGP
/outbound/NNA2FAIR_INVOC.2014_07_10_0159.TXT.PGP


/outbound/NNA2FAIR_INVOC.2014_07_10_0132.TXT.PGP  would be the oldest
Title: Re: Suggestions for an SFTP process
Post by: td on September 22, 2014, 02:21:10 PM
If you can assume that the oldest last write time for a file also means that it is the oldest file then the 'GetFileInfo' method of the Session object looks interesting.

The method can be found here:

http://winscp.net/eng/docs/library_session_getfileinfo#parameters (http://winscp.net/eng/docs/library_session_getfileinfo#parameters)

and the method's return class here:

http://winscp.net/eng/docs/library_remotefileinfo (http://winscp.net/eng/docs/library_remotefileinfo)

The 'ListDirectory' method might be an even better choice:

http://winscp.net/eng/docs/library_session_listdirectory (http://winscp.net/eng/docs/library_session_listdirectory)