Getting Error code 5: bad met when moving a file on Sftp
Hi,
I have a winscp c# script that I found from the net. The script continues to check ftp server and download the file to local when any new file is placed on Sftp. After sucessfull download the file is moved to other folder in Sftp. That is when I'm getting this error with code 5 and bad message.But the error occurs randomly and if I restart the script it downloads and move the file successfully. Also In case of error the file downloaded is 0kb.
param (
$localPath = "C:\FAB\OUTWARD",
$remotePath = "/OUTWARD/",
$remotePath2 = "/OUTWARD.DONE/"
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::ftp
HostName = "##"
UserName = "####"
Password = "#####"
SshHostKeyFingerprint = "JXVeWj46RCrJDqG3+Reu6Gz1BOQ="
}
$session = New-Object WinSCP.Session
try
{
$session.SessionLogPath = "D:\winscp.log"
# Connect
$session.Open($sessionOptions)
while ($True)
{
Write-Host "Synchronizing changes..."
# Synchronize files to local directory, collect results
$synchronizationResult = $session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $False)
# Deliberately not calling $synchronizationResult.Check
# as that would abort our script on any error.
# We will find any error in the loop below
# (note that $synchronizationResult.Downloads is the only operation
# collection of SynchronizationResult that can contain any items,
# as we are not removing nor uploading anything)
# Iterate over every download
foreach ($download in $synchronizationResult.Downloads)
{
# Success or error?
if ($download.Error -eq $Null)
{
Write-Host "Download of $($download.FileName) succeeded, moving from source"
# Download succeeded, remove file from source
$filename = [WinSCP.RemotePath]::EscapeFileMask($download.FileName)
$session.MoveFile($filename,$remotePath2)
}
else
{
Write-Host (
"Download of $($download.FileName) failed: $($download.Error.Message)")
}
}
$wait = [int]5
Write-Host "Waiting for $wait seconds, press Ctrl+C to abort..."
# Wait for 1 second in a loop, to make the waiting breakable
while ($wait -gt 0)
{
Start-Sleep -Seconds 1
$wait--
}
Write-Host
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
#exit 1
}
I have a winscp c# script that I found from the net. The script continues to check ftp server and download the file to local when any new file is placed on Sftp. After sucessfull download the file is moved to other folder in Sftp. That is when I'm getting this error with code 5 and bad message.But the error occurs randomly and if I restart the script it downloads and move the file successfully. Also In case of error the file downloaded is 0kb.
param (
$localPath = "C:\FAB\OUTWARD",
$remotePath = "/OUTWARD/",
$remotePath2 = "/OUTWARD.DONE/"
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::ftp
HostName = "##"
UserName = "####"
Password = "#####"
SshHostKeyFingerprint = "JXVeWj46RCrJDqG3+Reu6Gz1BOQ="
}
$session = New-Object WinSCP.Session
try
{
$session.SessionLogPath = "D:\winscp.log"
# Connect
$session.Open($sessionOptions)
while ($True)
{
Write-Host "Synchronizing changes..."
# Synchronize files to local directory, collect results
$synchronizationResult = $session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $False)
# Deliberately not calling $synchronizationResult.Check
# as that would abort our script on any error.
# We will find any error in the loop below
# (note that $synchronizationResult.Downloads is the only operation
# collection of SynchronizationResult that can contain any items,
# as we are not removing nor uploading anything)
# Iterate over every download
foreach ($download in $synchronizationResult.Downloads)
{
# Success or error?
if ($download.Error -eq $Null)
{
Write-Host "Download of $($download.FileName) succeeded, moving from source"
# Download succeeded, remove file from source
$filename = [WinSCP.RemotePath]::EscapeFileMask($download.FileName)
$session.MoveFile($filename,$remotePath2)
}
else
{
Write-Host (
"Download of $($download.FileName) failed: $($download.Error.Message)")
}
}
$wait = [int]5
Write-Host "Waiting for $wait seconds, press Ctrl+C to abort..."
# Wait for 1 second in a loop, to make the waiting breakable
while ($wait -gt 0)
{
Start-Sleep -Seconds 1
$wait--
}
Write-Host
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
#exit 1
}