How to skip file on error?
Hi, I have a PowerShell script to automate uploading of files. I am simulating upload errors by denying the script access permissions to one file out of a group of test files. The idea being that WinSCP will attempt to upload all files that match the filespec and if one file cannot be uploaded, the others will be. Afterwards the script analyses the transfer results and reports what files failed and why.
Problem is that WinSCP aborts when it encounters an error, the remaining files are not uploaded. My script will then retry uploading the filespec but now the file causing an error is the first to go and therefore WinSCP aborts again and never gets to the remaining files no matter additional retries. I want WinSCP to skip any problem file and continue to upload the remaining files.
I know I can script around this by handling the files individually but I would really like to upload all files in one session, not open and close individual sessions.
Here are my session settings, minus the site-specific information
The
SO How do I make that last "Abort" into a "Skip" so that the rest of the files are uploaded? Evidently the raw setting
Problem is that WinSCP aborts when it encounters an error, the remaining files are not uploaded. My script will then retry uploading the filespec but now the file causing an error is the first to go and therefore WinSCP aborts again and never gets to the remaining files no matter additional retries. I want WinSCP to skip any problem file and continue to upload the remaining files.
I know I can script around this by handling the files individually but I would really like to upload all files in one session, not open and close individual sessions.
Here are my session settings, minus the site-specific information
$FTPSessionOptions = New-Object WinSCP.SessionOptions $FTPSessionOptions.Protocol = [WinSCP.Protocol]::sftp $FTPSessionOptions.AddRawSettings("ContinueOnError","1") $FTPTransferOptions = New-Object WinSCP.TransferOptions $FTPTransferOptions.TransferMode = [WinSCP.TransferMode]::Binary $FTPTransferOptions.FilePermissions = $Null $FTPTransferOptions.PreserveTimestamp = $False Here is how I upload the files: try { $FTPSession = New-Object WinSCP.Session $FTPSession.Open($FTPSessionOptions) $TransferResult = $FTPSession.PutFiles("$TargetFolder\$FileFilter", "./", $TRUE, $FTPTransferOptions) } catch { #Log-Event is my own logging function Log-Event "Error" "Error Transferring Files: [$($FTPSession.output)]" 305 $False } finally { try { $FTPSession.Dispose() } catch { $Error.clear() } #i gnore any error from the above }
PutFiles
method does not return a trappable error – or for some other reason the catch block doesn't ever fire. Execution skips immediately to finally. The output of the session is:
winscp> option batch on batch on winscp> option confirm off confirm off winscp> open -hostkey="xxxxxxxxx" -timeout=15 "sftp://user:pw@hostname.com" -rawsettings ContinueOnError="1" Searching for host... Connecting to host... Authenticating... Using username "user". Authenticating with pre-entered password. Authenticated. Starting the session... Reading remote directory... Session started. Active session: [1] user@hostname.com winscp> put -delete -nopermissions -nopreservetime -transfer="binary" -- "c:\temp\*.tst" "./" Can't open file 'c:\temp\test41.tst'. System Error. Code: 5. Access is denied (A)bort, (R)etry, (S)kip, Ski(p) all: Abort
ContinueOnError
doesn't do that.