Differences

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

script_download_timestamped_filename 2015-02-18 script_download_timestamped_filename 2022-06-16 (current)
Line 1: Line 1:
-====== [[download_timestamped_filename]] Downloading file to timestamped-filename ======+====== Downloading file to timestamped-filename ======
-===== Using WinSCP .NET Assembly =====+===== [[library]] Using WinSCP .NET Assembly =====
Use [[library|WinSCP .NET assembly]] from your favorite language. Use relevant construct of your language or API of your runtime environment for the file name formatting. Use [[library|WinSCP .NET assembly]] from your favorite language. Use relevant construct of your language or API of your runtime environment for the file name formatting.
Line 19: Line 19:
    # Setup session options     # Setup session options
-    $sessionOptions = New-Object WinSCP.SessionOptions +    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ 
- ···$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp +········Protocol = [WinSCP.Protocol]::Sftp 
-   $sessionOptions.HostName = "example.com" + ·······HostName = "example.com" 
-   $sessionOptions.UserName = "user" + ·······UserName = "user" 
-   $sessionOptions.Password = "mypassword" + ·······Password = "mypassword" 
-   $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"+ ·······SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." 
 +    }
    $session = New-Object WinSCP.Session     $session = New-Object WinSCP.Session
Line 34: Line 35:
        # Format timestamp         # Format timestamp
-        $stamp = $(Get-Date -f "yyyyMMddHHmmss")+        $stamp = $(Get-Date -Format "yyyyMMddHHmmss")
        # Download the file and throw on any error         # Download the file and throw on any error
Line 49: Line 50:
    exit 0     exit 0
} }
-catch [Exception]+catch
{ {
-    Write-Host $_.Exception.Message+    Write-Host "Error: $($_.Exception.Message)"
    exit 1     exit 1
} }
</code> </code>
-===== Using WinSCP Scripting ===== +===== [[scripting]] Using WinSCP Scripting ===== 
-==== [[timestamp]] Using TIMESTAMP Variable ==== +In scripting, you can use ''[[scripting#timestamp|%TIMESTAMP%]]'' construct to insert a real-time to a script.
-//In the latest release//, &recent you can use ''[[scripting#timestamp|%TIMESTAMP%]]'' construct to insert a real-time to a script.+
<code winscp> <code winscp>
-option batch abort 
-option confirm off 
open mysession open mysession
get "/home/user/download.txt" "C:\downloaded\download.txt.%TIMESTAMP#yyyymmddhhnnss%" get "/home/user/download.txt" "C:\downloaded\download.txt.%TIMESTAMP#yyyymmddhhnnss%"
exit exit
-</code> 
- 
- 
-==== [[batch]] From a Batch File ==== 
-&recent (*remove once %TIMESTAMP% is widespread*) 
-In Windows batch file, you may retrieve current time in locale-independent format using command ''[[http://technet.microsoft.com/en-us/library/bb491034.aspx|wmic]] os get LocalDateTime''. You can parse the value using using [[http://en.wikibooks.org/wiki/Windows_Batch_Scripting#String_processing|string processing syntax]]: 
- 
-<code batch> 
-@echo off 
- 
-for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set LDT=%%j 
-set STAMP=%LDT:~0,4%%LDT:~4,2%%LDT:~6,2%%LDT:~8,2%%LDT:~10,2%%LDT:~12,2% 
- 
-winscp.com /script=script.txt 
-</code> 
- 
-//Note that the expression after ''set STAMP='' shows how to extract individual date/time components, in case you need to interleave them by some punctuation. If not, you can replace it by simple ''%LDT:~0,14%''.// 
- 
-The ''script.txt'' should make use of the variable ''STAMP'' using ''%STAMP%'' [[scripting#variables|syntax]]. For example: 
- 
-<code winscp> 
-option batch abort 
-option confirm off 
-open mysession 
-get "/home/user/download.txt" "C:\downloaded\download.txt.%STAMP%" 
-exit 
-</code> 
- 
-==== From a JScript ==== 
-&recent (*remove once %TIMESTAMP% is widespread*) 
-You may use following [[guide_automation_advanced#wsh|Windows script host JScript code]] (''example.js''): 
- 
-<code javascript> 
-// Local path to download to (keep trailing slash) 
-var LOCALPATH = "c:\\downloaded\\"; 
-// Remote path to download from (keep trailing slash) 
-var REMOTEPATH = "/home/user/"; 
-// File to download 
-var FILE = "download.txt"; 
-// Session to connect to 
-var SESSION = "session"; 
-// 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) + 
-    pad(date.getHours(), 2) + 
-    pad(date.getMinutes(), 2) + 
-    pad(date.getSeconds(), 2); 
- 
-var shell = WScript.CreateObject("WScript.Shell"); 
- 
-// 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 + FILE + "." + stamp + "\"\n" + 
-    "exit\n"); 
- 
-// wait until it finishes and collect its output 
-var output = exec.StdOut.ReadAll(); 
-// optionally print the output 
-WScript.Echo(output); 
-</code> 
- 
-Run the script with command: 
-<code batch> 
-cscript /nologo example.js 
</code> </code>

Last modified: by martin