Differences
This shows you the differences between the selected revisions of the page.
2014-11-20 | 2014-11-20 | ||
upload_most_recent_file (martin) | creating upload_most_recent_file (martin) | ||
Line 384: | Line 384: | ||
===== [[upload_most_recent_file]] Uploading the most recent file ===== | ===== [[upload_most_recent_file]] Uploading the most recent file ===== | ||
+ | |||
+ | ==== [[library]] Using WinSCP .NET Assembly ==== | ||
+ | Use [[library|WinSCP .NET assembly]] from your favorite language. | ||
+ | |||
+ | If you do not have your favorite language, use [[library_powershell|PowerShell]]: | ||
+ | |||
+ | <code powershell> | ||
+ | try | ||
+ | { | ||
+ | # Load WinSCP .NET assembly | ||
+ | Add-Type -Path "WinSCPnet.dll" | ||
+ | |||
+ | # Setup session options | ||
+ | $sessionOptions = New-Object WinSCP.SessionOptions | ||
+ | $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp | ||
+ | $sessionOptions.HostName = "example.com" | ||
+ | $sessionOptions.UserName = "user" | ||
+ | $sessionOptions.Password = "mypassword" | ||
+ | $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | ||
+ | |||
+ | $session = New-Object WinSCP.Session | ||
+ | |||
+ | try | ||
+ | { | ||
+ | # Connect | ||
+ | $session.Open($sessionOptions) | ||
+ | |||
+ | $localPath = "c:\toupload\" | ||
+ | $remotePath = "/home/user/" | ||
+ | |||
+ | # Gel list of files in the directory | ||
+ | $directoryInfo = $session.ListDirectory($remotePath) | ||
+ | |||
+ | # Select the most recent file. | ||
+ | # The !$_.PsIsContainer test excludes subdirectories. | ||
+ | # With PowerShell 3.0, you can replace this with -File switch of Get-ChildItem. &win8 | ||
+ | $latest = | ||
+ | Get-ChildItem -Path $localPath | | ||
+ | Where-Object {!$_.PsIsContainer} | | ||
+ | Sort-Object LastWriteTime -Descending | | ||
+ | Select-Object -First 1 | ||
+ | |||
+ | # Any file at all? | ||
+ | if ($latest -eq $Null) | ||
+ | { | ||
+ | Write-Host "No file found" | ||
+ | exit 1 | ||
+ | } | ||
+ | |||
+ | # Upload the selected file | ||
+ | $session.PutFiles($session.EscapeFileMask($localPath + $latest.Name), $remotePath).Check() | ||
+ | } | ||
+ | finally | ||
+ | { | ||
+ | # Disconnect, clean up | ||
+ | $session.Dispose() | ||
+ | } | ||
+ | |||
+ | exit 0 | ||
+ | } | ||
+ | catch [Exception] | ||
+ | { | ||
+ | Write-Host $_.Exception.Message | ||
+ | exit 1 | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | ==== Using WinSCP Scripting ==== | ||
+ | You may use following batch file: | ||
+ | |||
+ | <code batch> | ||
+ | @echo off | ||
+ | set SESSION=sftp://user:password@example.com/ | ||
+ | set LOCAL_PATH=c:\toupload\ | ||
+ | set REMOTE_PATH=/home/user/ | ||
+ | |||
+ | for /f "delims=" %%i in ('dir /b /od %LOCAL_PATH%\*') do set LATEST=%%i | ||
+ | if "%LATEST%" == "" goto NO_FILE | ||
+ | |||
+ | winscp.com /command ^ | ||
+ | "option batch abort" ^ | ||
+ | "option confirm off" ^ | ||
+ | "open %SESSION%" ^ | ||
+ | "put %LOCAL_PATH%%LATEST% %REMOTE_PATH%" ^ | ||
+ | "exit" | ||
+ | exit %ERRORLEVEL% | ||
+ | |||
+ | :NO_FILE | ||
+ | echo No file found | ||
+ | exit 1 | ||
+ | </code> | ||
===== [[checking_file_existence]] Checking file existence ===== | ===== [[checking_file_existence]] Checking file existence ===== |