Moving local files to different location with timestamp after successful upload

Skumar_CCSA
Skumar_CCSA used Ask the Experts™
on
Hi ,

I found this script it is useful saving my time.
i want help from experts...
after successful transfer it moving the file to archive..but every week the activity will go and chances for overwrite.
I want to have the dateandtime stamp to put while moving the files to archive..
can help to modify the code pls..

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

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
    {
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David FavorFractional CTO
Distinguished Expert 2018

Commented:
If you have many files to move tar or rsync both preserve all file ownership + permissions + timestamps.

Author

Commented:
This is for upload files to ftp..
want the timestamp to put moving file.

Author

Commented:
pls help....
Move-Item $transfer.FileName $backupPath
I want the moving file to have timestamp..
Either way fine
testfile.xml.zip.p7_DDMMYYYY_HHMMSS
testfile_DDMMYYYY_HHMMSS.xml.zip.p7
i got it and able to do myself.
Now i can see the file moved to archive after upload and moved file with datetime
             $fileWithOutExt = [io.path]::GetFileNameWithoutExtension($transfer.FileName)
                $MoveFile1 = "$backupPath\$($fileWithOutExt)_$date.p7"
                Move-Item $transfer.FileName $MoveFile1

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial