How to retreive a file from remote SFTP Site

Advertisement

Drakul663
Guest

How to retreive a file from remote SFTP Site

Hello, I am trying to retrieve a specific file with today's time-stamp and download it to a network share. I am using the Cscript provided by WinSCP and am unable to get the file;

--------------------------------
// Configuration
 
// Local path to download to (keep trailing slash)
var LOCALPATH = "c:\\downloaded\\";
// Remote path to search in (keep trailing slash)
var REMOTEPATH = "remote_filename_<TodaysDate>.zip";
// Mask of files to search for
var FILEMASK = "*.*";
// Session to connect to
var SESSION = "remote.ftp.site";
// Path to winscp.com
var WINSCP = "c:\\program files\\winscp\\winscp.com";
 
var filesys = WScript.CreateObject("Scripting.FileSystemObject");
var shell = WScript.CreateObject("WScript.Shell");
   
var logfilepath = filesys.GetSpecialFolder(2) + "\\" + filesys.GetTempName() + ".xml";
 
var exec;
 
// run winscp to get list of file in the remote directory into XML log
exec = shell.Exec("\"" + WINSCP + "\" /xmllog=\"" + logfilepath + "\"");
exec.StdIn.Write(
    "option batch abort\n" +
    "open \"" + SESSION + "\"\n" +
    "ls \"" + REMOTEPATH + FILEMASK + "\"\n" +
    "exit\n");
 
// wait until it finishes and collect its output
var output = exec.StdOut.ReadAll();
// optionally print the output
WScript.Echo(output);
 
if (exec.ExitCode != 0)
{
    WScript.Echo("Error retrieving list of files");
    WScript.Quit(1);
}
 
// look for log file
var logfile = filesys.GetFile(logfilepath);
 
if (logfile == null)
{
    WScript.Echo("Cannot find log file");
    WScript.Quit(1);
}
 
// parse XML log file
var doc = new ActiveXObject("MSXML2.DOMDocument");
doc.async = false;
doc.load(logfilepath);
 
doc.setProperty("SelectionNamespaces", 
    "xmlns:w='http://winscp.net/schema/session/1.0'");
 
var nodes = doc.selectNodes("//w:file");
 
// find the latest file
var filenameLatest = null;
var modificationLatest = null;
for (var i = 0; i < nodes.length; ++i)
{
    var filename = nodes[i].selectSingleNode("w:filename/@value");
    var modification = nodes[i].selectSingleNode("w:modification/@value");
    if ((filename != null) &&
        (filename.value != ".") &&
        (filename.value != "..") &&
        (modification != null))
    {
        // can compare timestamps stringwise
        if ((modificationLatest == null) ||
            (modificationLatest < modification.value))
        {
            modificationLatest = modification.value;
            filenameLatest = filename.value;
        }
    }
}
 
// no file in the log
if (filenameLatest == null)
{
    WScript.Echo("No file found");
    WScript.Quit(0);
}
 
// run winscp to download the latest file
exec = shell.Exec("\"" + WINSCP + "\"");
exec.StdIn.Write(
    "option batch abort\n" +
    "option confirm off\n" +
    "open \"" + SESSION + "\"\n" +
    "get \"" + REMOTEPATH + filenameLatest + "\" \"" + LOCALPATH + "\"\n" +
    "exit\n");
 
// wait until it finishes and collect its output
var output = exec.StdOut.ReadAll();
// optionally print the output
WScript.Echo(output);
 
if (exec.ExitCode != 0)
{
    WScript.Echo("Error downloading " + filenameLatest);
    WScript.Quit(1);
}

-------------------------------

Output shows the following;

winscp> ls "remote_filename*.*"
----------   0                      477106 Jun 20  8:32:22 2016 remote_filename.zip
----------   0                      462312 Jun 19  8:12:03 2016 remote_filename_191.zip
----------   0                      524716 Jun 20 18:51:58 2016 remote_filename_2.zip
----------   0                      524710 Jun 20 19:12:16 2016 remote_filename_20160620.zip
----------   0                      517045 Jun 21  8:22:22 2016 remote_filename_20160621.zip
----------   0                      514129 Jun 22  8:22:15 2016 remote_filename_20160622.zip
----------   0                      506635 Jun 23  8:22:16 2016 remote_filename_20160623.zip
----------   0                      505781 Jun 24  8:21:43 2016 remote_filename_20160624.zip
----------   0                      505202 Jun 25  8:51:48 2016 remote_filename_20160625.zip
----------   0                      501444 Jun 26  8:22:09 2016 remote_filename_20160626.zip
----------   0                      506182 Jun 27  8:22:04 2016 remote_filename_20160627.zip
----------   0                      494650 Jun 28  8:22:32 2016 remote_filename_20160628.zip
winscp> exit

winscp> option batch abort
batch           abort
reconnecttime   120
winscp> option confirm off
confirm         off
winscp> open "remote.ftp.site"
Searching for host...
Connecting to host...
Authenticating...
Using username "FTP_User".
Authenticating with public key "rsa-key-2xxxxxx7".
Authenticated.
Starting the session...
Session started.
Active session: [1] remote.ftp.site
winscp> get "remote_filenameremote_filename_20160628.zip" "c:\downloaded\"
Can't get attributes of file 'remote_filenameremote_filename_20160628.zip'.
No such file or directory.
Error code: 2
Error message from server (en): The system cannot find the file specified.

Error downloading remote_filename_20160628.zip

Reply with quote

Advertisement

BT
Guest

I created a batch file for this, but for some reason, the prompt just sits open and does not retreive the files;

############ WinSCP.BAT##########
winscp.com /command "open user@site:/<subdirectory" "option confirm off" /privatekey=C:\PrivateKey.ppk
get report_1_#yyyymmdd%".zip
get report_2_%TIMESTAMP#yyyymmdd%.zip
#################################

OUTPUT from remote site;
Searching for host...
Connecting to host...
Authenticating...
Using username "user".
Authenticating with public key "rsa-key-2xxxxx7".
Authenticated.
Starting the session...
Reading remote directory...
Session started.
Active session: [1] user@site
confirm off
winscp>

Reply with quote

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

Re: How to retreive a file from remote SFTP Site

Drakul663 wrote:

Hello, I am trying to retrieve a specific file with today's time-stamp and download it to a network share. I am using the Cscript provided by WinSCP and am unable to get the file
I just do not understand your code. Why there is is remote_filename_<TodaysDate>.zip in the REMOTEPATH? That's clearly the problem. Anyway, your problem does not seem to be with WinsCP, but rather with your code. Moreover the output does not look like, you have actually used this input.

Reply with quote

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

BT wrote:

winscp.com /command "open user@site:/<subdirectory" "option confirm off" /privatekey=C:\PrivateKey.ppk
get report_1_#yyyymmdd%".zip
get report_2_%TIMESTAMP#yyyymmdd%.zip
It should be

winscp.com /command "open user@site:/<subdirectory -privatekey=C:\PrivateKey.ppk" "get report_1_#yyyymmdd%".zip" "get report_2_%%TIMESTAMP#yyyymmdd%%.zip" "exit"

Reply with quote

Advertisement

You can post new topics in this forum