By: Eskalin in Javascript Tutorials on 2012年10月24日 [フレーム]
Normally winscp program is used to connect to a remote machine and transfer files. Recently I wanted to connect to my linux machine from a windows machine to download a file automatically everyday. The linux machine was generating daily log files and the name of the log file keeps changing as the current date. So i wrote this script to connect to that machine remotely and download the file every day by running the script in a scheduler.
Save the below as test.js
Change the path to the winscp.com file based on where you installed your winscp program,
Then run the file as
cscript /nologo test.js
//Scripts starts from here.
// Local path to download to (keep trailing slash) var LOCALPATH = "d:\\NASShare\\C5\\billlog\\"; // Remote path to download from (keep trailing slash) var REMOTEPATH = "/var/log/sipproxyd/billing/"; // Session to connect to var SESSION = "root:[email protected]"; // Path to winscp.com var WINSCP = "c:\\program files\\winscp\\winscp.com"; // helper function to pad zeroes to the left of number function pad(n, len) { var s = n.toString(); while (s.length < len) { s = '0' + s; } return s; } var date = new Date(); // format timestamp var stamp = pad(date.getFullYear(), 4) + '-' + pad(date.getMonth(), 2) + '-' + pad(date.getDate(), 2); // File to download var FILE = "billing.log." + stamp; var shell = WScript.CreateObject("WScript.Shell"); //WScript.echo(FILE); run winscp to download the file into timestamped-filename exec = shell.Exec("\"" + WINSCP + "\""); exec.StdIn.Write( "option batch abort\n" + "open \"" + SESSION + "\"\n" + "get \"" + REMOTEPATH + FILE + "\" \"" + LOCALPATH + "\"\n" + "exit\n"); // wait until the script finishes while (exec.Status == 0) { WScript.Sleep(100); WScript.Echo(exec.StdOut.ReadAll()); }
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Javascript )
Dynamically modify the option set in Dynamics 365 forms
promise and .then() in JavaScript
reduce() and filter() in JavaScript
History and evolution of Javascript
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Show how many characters remaining in a html text box using javascript
Latest Articles (in Javascript)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate