Re: Missing files after transfer from local to remote using PowerShell script
You have the logs. You can show them that the specific file that they claim to be missing was indeed uploaded to their server.
Before posting, please read how to report bug or request support effectively.
Bug reports without an attached log file are usually useless.
param (
$localPath = "F:\sap-files\production\out\*",
$remotePath = "/RFID/OUT/",
$backupPath = "F:\sap-files\production\out_arch\",
$log_file = "F:\sap-files\production\log.txt"
)
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 = "ftp_ip"
UserName = "ftp_user"
Password = "ftp_password"
}
$session = New-Object WinSCP.Session
try {
# Connect
$session.Open($sessionOptions)
# Upload files, collect results
$transferResult = $session.PutFiles($localPath, $remotePath)
# Iterate over every transfer
foreach ($transfer in $transferResult.Transfers) {
# Success or error?
if ($transfer.Error -eq $Null) {
$msg_success_upload = "$(Get-Date) - Upload of $($transfer.FileName) succeeded!"
Write-Host $msg_success_upload
# Add to log
Add-Content -Path $log_file -Value $msg_success_upload
# Upload succeeded, move source file to backup
$msg_success_move_to_backup = "$(Get-Date) - Backup of $($transfer.FileName) succeeded!"
Write-Host $msg_success_move_to_backup
Move-Item $transfer.FileName $backupPath -force
# Add to log
Add-Content -Path $log_file -Value $msg_success_move_to_backup
}
else {
$msg_error_upload = "$(Get-Date) - Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
# Add to log
Add-Content -Path $log_file -Value $msg_error_upload
}
}
}
catch {
$msg_ftp_not_connect = "$(Get-Date) - Connection error!"
Write-Host $msg_ftp_not_connect
# Add to log
Add-Content -Path $log_file -Value $msg_ftp_not_connect
}
finally {
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch {
$msg_except_error = "Error: $($_.Exception.Message)"
Write-Host $msg_except_error
# Add to log
Add-Content -Path $log_file -Value $msg_except_error
exit 1
}