Moving Files that have been successfully uploaded

Advertisement

JimmyFree
Joined:
Posts:
10

Moving Files that have been successfully uploaded

(Perhaps I should post here for help?)

There is a script that permits Moving Files to New Directory After Successful Upload. It is here: https://winscp.net/eng/docs/script_local_move_after_successful_upload

When I run it, it reports success, but the file never shows at its destination.

When I transferred using the GUI, it said it was successful, but had an error because it could not set the timestamp. When I turned off preserving timestamp in the GUI, then transfer worked.

How do I modify the above script so that it turns off setting the timestamp?

THANKS!

Reply with quote

Advertisement

JimmyFree
Joined:
Posts:
10

jrp78 wrote:

https://winscp.net/eng/docs/message_preserve_time_perm

Thanks for your reply.

I am aware of this posting. Unfortunately I do not know how to deploy it within the script that I mentioned. Can you show me?

Reply with quote

JimmyFree
Joined:
Posts:
10

jrp78 wrote:

Can you post your actual script?

param (
    $localPath = "C:\upload\*",
    $remotePath = "/home/user/",
    $backupPath = "C:\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 {0} succeeded, moving to backup" -f
                    $transfer.FileName)
                # Upload succeeded, move source file to backup
                Move-Item $transfer.FileName $backupPath
            }
            else
            {
                Write-Host ("Upload of {0} failed: {1}" -f
                    $transfer.FileName, $transfer.Error.Message)
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host ("Error: {0}" -f $_.Exception.Message)
    exit 1
}

It reports that it works, but I don't see the file appearing on the other end. I know the destination server will not permit permission and timestamp changes. So I need to disable these.

Thanks much for your help!

Reply with quote

Advertisement

vinicostasantos
Joined:
Posts:
5
Location:
Brasil

Hi Jimmy,

I have this script working here.

I'll show you my script and maybe you can solve your problem:

*A tip: DoubleCheck your Winscp software version and Windows permissions.

My Winscp version is: 5.7.7

try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\WinSCP\WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
    $sessionOptions.HostName = "*****"
    $sessionOptions.UserName = "*****"
    $sessionOptions.Password = "*****"
    $sessionOptions.SshHostKeyFingerprint = "*****"
 
    $session = New-Object WinSCP.Session
try
    {
        # Connect
        $session.Open($sessionOptions)
 
        $localPath =  "******"
        $remotePath = "******"
        $backupPath = "******"
 
        # 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 {0} succeeded" -f
                    $transfer.FileName)
                # Upload succeeded, move source file to backup
                Move-Item $transfer.FileName $backupPath
            }
            else
            {
                Write-Host ("Upload of {0} failed: {1}" -f
                    $transfer.FileName, $transfer.Error.Message)
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    exit 1
}

Reply with quote

JimmyFree
Joined:
Posts:
10

[quote="vinicostasantos"]Hi Jimmy,

I have this script working here.

I'll show you my script and maybe you can solve your problem:

*A tip: DoubleCheck your Winscp software version and Windows permissions.

My Winscp version is: 5.7.7


Thanks very much! Will check all of this out now.

Reply with quote

Advertisement

JimmyFree
Joined:
Posts:
10

vinicostasantos wrote:


try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\WinSCP\WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
    $sessionOptions.HostName = "*****"
    $sessionOptions.UserName = "*****"
    $sessionOptions.Password = "*****"
    $sessionOptions.SshHostKeyFingerprint = "*****"
 
    $session = New-Object WinSCP.Session
try
    {
        # Connect
        $session.Open($sessionOptions)
 
        $localPath =  "******"
        $remotePath = "******"
        $backupPath = "******"
 
        # 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 {0} succeeded" -f
                    $transfer.FileName)
                # Upload succeeded, move source file to backup
                Move-Item $transfer.FileName $backupPath
            }
            else
            {
                Write-Host ("Upload of {0} failed: {1}" -f
                    $transfer.FileName, $transfer.Error.Message)
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    exit 1
}

Thanks for your help. This script works.

I wonder, however, how it may be edited to rename the uploaded files before archiving them. This is important because if the file to be ARCHIVED has the same name as another ARCHIVED file, the archive fails.
Last edited by JimmyFree on 2016-11-16 03:13; edited 4 times in total

Reply with quote

martin
Site Admin
martin avatar

JimmyFree wrote:

I wonder, however, how it may be edited to rename the uploaded files before archiving them. This is important because if the file to be uploaded has the same name as another file, the upload fails.
Before archiving or before uploading?

Reply with quote

Guest

martin wrote:

JimmyFree wrote:

I wonder, however, how it may be edited to rename the uploaded files before archiving them. This is important because if the file to be uploaded has the same name as another file, the upload fails.
Before archiving or before uploading?

Before Archiving.

Reply with quote

JimmyFree
Joined:
Posts:
10

martin wrote:

JimmyFree wrote:

I wonder, however, how it may be edited to rename the uploaded files before archiving them. This is important because if the file to be uploaded has the same name as another file, the upload fails.
Before archiving or before uploading?

Sorry. Thought I was logged in.

I would like the file renamed before archiving it - appending a timestamp would be awesome.

Really curious to see how this is done.

THANKS!!

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum