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

JimmyFree

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
JimmyFree

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!!
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.
martin

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?
JimmyFree

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.
JimmyFree

[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.
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

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

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
JimmyFree

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!
jrp78

Can you post your actual script?
JimmyFree

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?
JimmyFree

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!