Re: PowerShell script, Transfer to SFTP and then rename the local file and move to another folder
Already answered here:
Renaming and move file
Renaming and move file
ASSOCIATIONS_IT
ArticleL1_declaration.xlsx
ArticleL2_declaration.xlsx
ArticleL3_declaration.xlsx
↳ ARCHIVE
CATALOG
ArticleL1_catalogs.xlsx
ArticleL2_catalogs.xlsx
ArticleL3_catalogs.xlsx
↳ ARCHIVE
ITEM
ArticleL1_Master_xxx.xlsx
ArticleL2_Master_xxx.xlsx
ArticleL3_Master_xxx.xlsx
↳ ARCHIVE
.xslx
.
.xslx
.
ARCHIVE
child folder.
Name.timestamp.xlsx
.
param (
$localPath = "C:\WEBORDER\ASSOCIATIONS_IT\Article*.xlsx",
$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 = "hostftp"
UserName = "username"
Password = "password"
}
$session = New-Object WinSCP.Session
$session.SessionLogPath = "C:\WEBORDER\LOGS\incremental_LOG.log"
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)
{
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
}