Moving local files to different location with timestamp after successful upload

Advertisement

Skumar_CCSA
Joined:
Posts:
1
Location:
Singapore

Moving local files to different location with timestamp after successful upload

Thanks to WinScp team providing few scripts.
I am new to power shell request help how to put timestamp while moving file to another directory after successful upload.

example
Localpathfile : E:\ABC\Zip_Source\testfile.xml.zip.p7
backupPath = "E:\ABC\Backup\testfile.xml.zip.p7_DDMMYYYY_HHMMSS

I giving below code here from winscp sample scripts.Script from internet

# Upload Files to FTP and move source files to archive
param (
    $localPath = "E:\ABC\Zip_Source\*.p7",
    $remotePath = "/home/user/",
    $backupPath = "E:\ABC\Backup\"
)
 
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "example.com"
        UserName = "user"
        Password = "mypassword"
        SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
    }
 
    $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)
            {
                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
    {

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum