Problem with moving files from remote folder to local folder
Hello everyone!
I am creating a script that will help me do the following:
1. Copy files from remote folder A to local folder B.
2. If transfer (step 1) is successful, move files in remote folder A to local folder C. (archive folder)
If transfer (step 1) is unsuccessful, move files in remote folder A to local folder D. (error folder)
I do not have any problems copying the files (step 1). I am having trouble performing the move operation. I am trying to use the Session.GetFiles method within a loop, as you can see in the code below. I get no errors, but I see that nothing is moved. I would appreciate any help or suggestion.
I am creating a script that will help me do the following:
1. Copy files from remote folder A to local folder B.
2. If transfer (step 1) is successful, move files in remote folder A to local folder C. (archive folder)
If transfer (step 1) is unsuccessful, move files in remote folder A to local folder D. (error folder)
I do not have any problems copying the files (step 1). I am having trouble performing the move operation. I am trying to use the Session.GetFiles method within a loop, as you can see in the code below. I get no errors, but I see that nothing is moved. I would appreciate any help or suggestion.
$period = [TimeSpan]::FromSeconds(20) $lastRunTime = [DateTime]::MinValue while (1) { while ((Get-Date) - $lastRunTime -lt $period) { Start-Sleep -Milliseconds 5000 Write-Host "Waiting for new files..." } $lastRunTime = Get-Date 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]::Sftp HostName = "BLABLABLA" UserName = "BLABLABLA" Password = "BLABLABLA" GiveUpSecurityAndAcceptAnySshHostKey = "true" } $session = New-Object WinSCP.Session try { # Connect $session.Open($sessionOptions) # Set transfer options $remotePath = "/usr/BLABLA/REMOTEPATH/" $transferOptions = New-Object WinSCP.TransferOptions $transferOptions.TransferMode = [WinSCP.TransferMode]::Ascii $transferResult = $session.GetFiles("/usr/BLABLA/REMOTE PATH/*.txt", "C:\Users\BLABLA\LOCALFOLDER-A\", $False, $transferOptions) $transferResult.Check() foreach ($transfer in $transferResult.Transfers) { if ($transferResult.IsSuccess) { # Download success message Write-Host ("File {0} was downloaded successfully..." -f $transfer.FileName.TrimStart("/usr/BLABLA/REMOTE PATH/")) $FileName = $transfer.FileName.TrimStart("/usr/BLABLA/REMOTE PATH/") $A = (get-date (get-date) -UFormat "%Y-%m%-%d %H:%M") Add-Content C:\Users\BLABLA\LOCALFOLDER-A\Log\LogFile.log "$FileName | $A | Downloaded" #MOVE OPERATION IS HERE: (REMOTE TO LOCAL-B (ARCHIVE FOLDER)) $transferResult2 = $session.GetFiles("/usr/BLABLA/REMOTE PATH/*.txt", "C:\Users\BLABLA\LOCALFOLDER-B\", $True, $transferOptions) } elseif (!$transferResult.IsSuccess) { #MOVE OPERATION IS HERE: (REMOTE TO LOCAL-C (ERROR FOLDER)) $transferResult3 = $session.GetFiles("/usr/BLABLA/REMOTE PATH/*.txt", "C:\Users\BLABLA\LOCALFOLDER-C", $True, $transferOptions) # Download error message Write-Host ("Error downloading file {0}!" -f $transfer.FileName, $transferResult.Failures[0].Message) $B = (get-date (get-date) -UFormat "%Y-%m%-%d %H:%M") Add-Content C:\Users\BLABLA\LOCALFOLDER-A\Log\LogFile.log "$FileName | $A | Download error", $transferResult.Failures[0].Message } } } finally { # Disconnect, clean up $session.Dispose() } } catch [Exception] { Write-Host ("Error: {0}" -f $_.Exception.Message) } Start-Sleep -s 10 }