Topic "Moving Files that have been successfully uploaded"

Author Message
JimmyFree
[View user's profile]

Joined: 2016-10-26
Posts: 10
(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!
Advertisements
jrp78
[View user's profile]

Joined: 2016-10-07
Posts: 16
Location: SoVA
https://winscp.net/eng/docs/message_preserve_time_perm
JimmyFree
[View user's profile]

Joined: 2016-10-26
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?
jrp78
[View user's profile]

Joined: 2016-10-07
Posts: 16
Location: SoVA
Can you post your actual script?
JimmyFree
[View user's profile]

Joined: 2016-10-26
Posts: 10
jrp78 wrote:
Can you post your actual script?


Code:
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!
JimmyFree
[View user's profile]

Joined: 2016-10-26
Posts: 10
At least one other user is having this problem. I posted here: https://winscp.net/forum/viewtopic.php?p=73798#73798

As you see, in response, I posted the link that you posted to me. I just don't know how to use the information.

-JF
vinicostasantos
[View user's profile]

Joined: 2016-10-26
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

Code:
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
}
JimmyFree
[View user's profile]

Joined: 2016-10-26
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.
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26299
Location: Prague, Czechia
Your duplicate post:
https://winscp.net/forum/viewtopic.php?t=23939
JimmyFree
[View user's profile]

Joined: 2016-10-26
Posts: 10
vinicostasantos wrote:

Code:
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
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26299
Location: Prague, Czechia
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?
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.
JimmyFree
[View user's profile]

Joined: 2016-10-26
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!!
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26299
Location: Prague, Czechia
So just move it to a timestamped name, like:

Move-Item $transfer.FileName ($backupPath + (Get-Date -Format "yyyy-MM-dd"))

See also https://winscp.net/eng/docs/script_formatting_timestamp_batch_file
JimmyFree
[View user's profile]

Joined: 2016-10-26
Posts: 10
martin wrote:
So just move it to a timestamped name, like:

Move-Item $transfer.FileName ($backupPath + (Get-Date -Format "yyyy-MM-dd"))

See also https://winscp.net/eng/docs/script_formatting_timestamp_batch_file


Thanks very much. Didn't know it was so simple. I will try it out tonight.

Thanks again.

-JF
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License