Differences

This shows you the differences between the selected revisions of the page.

2009-04-23 2009-04-23
guide_automation_advanced#wsh (martin) timestamp file name with wsh (martin)
Line 29: Line 29:
===== Downloading file to timestamped-filename ===== ===== Downloading file to timestamped-filename =====
-==== Using local-side date printing tool ====+You may use following [[guide_automation_advanced#wsh|Windows script host JavaScript code]] (''example.js''):
-You can use any way to produce date string in desired format on command line, store that string to environment variable and use it in script. Although it is possible to [[http://www.robvanderwoude.com/datetiment.html|produce almost any date string]] using Windows command-line tools, easier is to use dedicated tool, such as [[http://www.huweb.hu/maques/realdate.htm|realdate]]:+<code javascript> 
 +// Local path to download to (keep trailing slash) 
 +var LOCALPATH = &quot;.\\"; 
 +// Remote path to download from (keep trailing slash
 +var REMOTEPATH = &quot;/"; 
 +// File to download 
 +var FILE = &quot;winscp421.exe&quot;; 
 +// Session to connect to 
 +var SESSION = &quot;test@127.0.0.1"; 
 +// Path to winscp.com 
 +var WINSCP = &quot;c:\\program files\\winscp\\winscp.com&quot;;
-First create wrapper batch file to store the date string into environment variable:+// 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; 
 +}
-<code> +var date = new Date();
-for /f %%T in ('realdate.com /f="CCYYMMDDhhmmss"') do (set TIMESTAMP=%%T) +
-winscp.com /script=example.txt +
-</code>;+
-The script ''example.txt'' can use syntax ''%TIMESTAMP%'' to retrieve the date string in ''TIMESTAMP'' environment variable:+// format timestamp 
 +var stamp =  
 +    pad(date.getFullYear(), 4)
 + ···pad(date.getMonth(), 2)
 +   pad(date.getDate(), 2) + 
 +    pad(date.getHours(), 2)
 + ···pad(date.getMinutes(), 2)
 +    pad(date.getSeconds(), 2);
-<code winscp> +var shell = WScript.CreateObject(&quot;WScript.Shell&quot;);
-option batch abort +
-option confirm off +
-open session +
-get /home/user/examplefile.txt *.%TIMESTAMP%.txt +
-exit +
-&lt;/code>;+
-==== Using local-side scripting ====+// run winscp to get list of file in the remote directory into XML log 
 +exec = shell.Exec("\"" + WINSCP + &quot;\""); 
 +exec.StdIn.Write( 
 +   "option batch abort\n" + 
 +    "open \"" + SESSION + "\"\n" + 
 +    "get \"" + REMOTEPATH + FILE + "\" \"" + LOCALPATH + FILE + "." + stamp + "\"\n" + 
 +    "exit\n");
-You can use any available scripting language you have on the local host to generate appropriate WinSCP script. Following example uses PHP language: +// wait until the script finishes 
- +while (!exec.StdOut.AtEndOfStream) 
-<code php&gt; +{ 
-option batch abort + ···WScript.Echo(exec.StdOut.ReadAll()); 
-option confirm off +}
-open session +
-get /home/user/examplefile.txt *.<?=date("YmdHis")?>;.txt +
-exit+
</code> </code>
-When executed, the generated WinSCP script file may look like: +Run the script with command:
- +
-<code winscp> +
-option batch abort +
-option confirm off +
-open session +
-get /home/user/examplefile.txt *.20060605090825.txt +
-exit +
-</code> +
- +
-Now pass the generated script file as input to WinSCP:+
<code> <code>
-php -q download.php | winscp.com /script="%temp%\download.tmp" +cscript /nologo example.js
-</code> +
- +
- +
-==== Using remote-side scripting ==== +
-If you do not have a scripting language on the local host, you can use remote-side script (like shell script). This approach requires opening separate [[shell session]] to invoke remote-side scripting. +
- +
-<code winscp> +
-option batch abort +
-option confirm off +
-open session +
-# Make copy of the remote file to temporary timestamped file. +
-# Also add unique extension to easily find the file in the temporary directory. +
-call cp /home/user/examplefile.txt /tmp/examplefile.`date +%Y%m%d%H%M%S`.unique +
-# Download all the files with the unique extension. +
-# There should be only one, the one just created. +
-# While downloading, remove the unique extension. +
-get /tmp/*.unique *. +
-# Remove the temporary file. +
-rm /tmp/*.unique +
-exit+
</code> </code>

Last modified: by martin