2017-05-25 30 views
1

我使用WinSCP連接到FTP並上傳一堆文件,但當遇到文件無法上載時遇到問題時該腳本停止,並且以下文件不會被上傳。我主要使用的代碼從WinSCP's documentation使用WinSCP .NET程序集批處理文件上傳停止,當單個文件上傳失敗

# Conectar 
$session.Open($sessionOptions) 

#Upload files 
$transferResult = $session.PutFiles($origenSubida, $destinoSubida) 

# Resultados de cada archivo 
foreach ($transfer in $transferResult.Transfers) 
{ 
    #looks if upload is correct 
    if ($transfer.Error -eq $Null) 
    { 
     LogWrite ("$(Get-Date) upload complete {0} , changing folder" -f $transfer.FileName) 
     Move-Item $transfer.FileName $rutaArchivos 
    } 
    else 
    { 
     LogWrite ("$(Get-Date) Upload of {0} has failed: {1}" -f $transfer.Filename, $transfer.Error.Message) 
    } 
} 

意外behabior如下:

我有得到上傳的文件夾,文件1,2,3和4的4個文件,如果我刪除所有來自文件3的權限並運行腳本,將文件1和文件2上載到FTP並將它們移動到另一個目錄,然後腳本以對文件3沒有權限的錯誤結束,文件4不會上載。

我不確定我是否忽略了任何東西,即使它不能對file3做任何事情,腳本是否應該繼續運行?我不確定是否有另一種方法可以使單個文件上傳失敗。

回答

0

如果您想要自定義錯誤處理,您必須自行循環文件。

$localPath = "C:\local\path" 
$remotePath = "/remote/path/" 

$localFiles = Get-ChildItem -Path $localPath 

foreach ($localFile in $localFiles) 
{ 
    Write-Host "Uploading $($localFile.FullName)..." 
    $sourcePath = [WinSCP.RemotePath]::EscapeFileMask($localFile.FullName) 
    $transferResult = $session.PutFiles($sourcePath, $remotePath) 

    if (!$transferResult.IsSuccess) 
    { 
     # Print error (but continue with other files) 
     Write-Host ("Error upload file $($localFile.FullName): " + 
      "$($transferResult.Failures[0].Message)") 
    }  
} 

同樣以什麼Recursively download directory tree with custom error handling官方的例子一樣。