Renaming and move file
Hi,
Please can someone help me with this script?
I have this script that upload some files to an FTP server.
After the upload I need to move this file to
Is possible?
Also is possible to implement the email alert in .NET assembly?
Here is the script
Please can someone help me with this script?
I have this script that upload some files to an FTP server.
After the upload I need to move this file to
Archive
folder and rename it with timestamp.
Is possible?
Also is possible to implement the email alert in .NET assembly?
Here is the script
param ( $localPath = "C:\WEBORDER\ASSOCIATIONS_IT\Article*", $remotePath = "/", $backupPath = "C:\WEBORDER\ASSOCIATIONS_IT\ARCHIVE\" ) 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 = "ftphost" UserName = "username" Password = "password" } $session = New-Object WinSCP.Session $session.SessionLogPath = "C:\WEBORDER\LOGS\incremental_LOG.log" try { # Connect $session.Open($sessionOptions) # Deliberately using an underscore instead of a dot, # as the dot has specific meaning in operation mask $suffix = "_filepart" $transferOptions = New-Object WinSCP.TransferOptions # Particularly with SFTP protocol, prevent additional .filepart suffix # from being added to uploaded files larger than 100 KB $transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::On # Upload all .xlsx files with temporary "_filepart" suffix $transferResult = $session.PutFiles(($localPath + "*.xlsx"), ($remotePath + "*.*" + $suffix), $False, $transferOptions) # Throw on any error $transferResult.Check() # Rename uploaded files foreach ($transfer in $transferResult.Transfers) { # Remove suffix $finalName = $transfer.Destination.SubString( 0, $transfer.Destination.Length - $suffix.Length) Write-Host "Renaming uploaded file $($transfer.Destination) to $finalName" # Rename uploaded file to its final name $session.MoveFile($transfer.Destination, $finalName) # Success or error? if ($transfer.Error -eq $Null) { Write-Host "Upload of $($transfer.FileName) succeeded, moving to backup" # Upload succeeded, move source file to backup Move-Item $transfer.FileName $backupPath } else { Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)" } } } finally { # Disconnect, clean up $session.Dispose() } exit 0 } catch { Write-Host "Error: $($_.Exception.Message)" exit 1 }