Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: Renaming and move file

For email notification please also check the Stack Overflow. That has nothing to do with WinSCP either.
vasy.ciuban

Re: Renaming and move file

I finally solved it like this.
But I have not solved with the email notification.
param (
    # Questi sono i parametri
    $localPath = "C:\WEBORDER\ASSOCIATIONS_IT\Article*",
    $remotePath = "/",
    $archivePath = "C:\WEBORDER\ASSOCIATIONS_IT\ARCHIVE\" +$(Get-Date -f "yyyyMMddHHmmss")
)
New-Item -ItemType directory -Path $archivePath
 
try
{
    # CARICO WinSCP .NET assembly
    Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
 
    # Sessione SFTP
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Ftp
        HostName = "server"
        UserName = "username"
        Password = "password"
    }
    $session = New-Object WinSCP.Session
    $session.SessionLogPath = "C:\WEBORDER\LOGS\incremental_LOG.log"
   
    try
    {
        # Mi Connetto
        $session.Open($sessionOptions)
      
        # Format timestamp
        $stamp = $(Get-Date -Format "yyyyMMddHHmmss")
 
        # Utilizzando deliberatamente un trattino basso invece di un punto,
        # poiché il punto ha un significato specifico nella maschera delle operazioni
        $suffix = "_filepart"
 
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::On
 
        # Carica tutti i file .xlsx con il suffisso "_filepart" temporaneo
        $transferResult =
            $session.PutFiles(($localPath + "*.xlsx"), ($remotePath + "*.*" + $suffix),
                $False, $transferOptions)
 
        $transferResult.Check()
 
        # Rinomina i file caricati
        foreach ($transfer in $transferResult.Transfers)
        {
            # Rumuovo il suffisso
            $finalName =
                $transfer.Destination.SubString(
                    0, $transfer.Destination.Length - $suffix.Length)
            Write-Host "Renaming uploaded file $($transfer.Destination) to $finalName"
            # Rinomina il file caricato con il suo nome finale
            $session.MoveFile($transfer.Destination, $finalName)
         
            # Success or error?
            if ($transfer.Error -eq $Null)
            {   
                Write-Host "Upload of $($transfer.FileName) succeeded, moving to Archive"
                # Upload succeeded, move source file to Archive
                Move-Item $transfer.FileName $archivePath
            }
            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
}
martin

Re: Renaming and move file

The code already does the move, doesn't it.
So is your question how to generate the timestamped name? That's not really WinSCP question, rather a generic PowerShell programming problem:
Timestamp on file name using PowerShell
The same with email alerts.
vasy.ciuban

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 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
}