Newbie needing some help moving from Filezilla...

Advertisement

Galant
Joined:
Posts:
2

Newbie needing some help moving from Filezilla...

Hi Guys,

Appreciate the assistance. I've recently taken over a project/client, part of which involves the automated daily transfer of a few files via FTP. Previously this had been done using Filezilla, however the client has been having problems with the automation and I've decided to ditch it and upon the recommendation of a friend use WinSCP.

I don't have a lot of experience on the scripting side of things, so although I think I've got the gist of things, I'm getting a little hung up on the nitty-gritty of it.

Since scripts already exist which had been used with Filezilla, I'm wondering if these will work. They're relatively simply, small scripts compared to others I've seen as examples, and so that's confusing me a bit.

There are two scripts:

1 - ftptransfer.txt
Login credentials, remote dir setup, multiple put and quit -

<username>
<password>
CD Outbox
ascii
prompt n
mput *.*
quit

2 - transfer.bat
Local Dir setp, Runs FTP based on other script creds, and archives the local files -

cd C:\Sales
ftp -s:C:\Sales\FTP_transfer\ftptransfer <ip address>
move *.* C:\Sales\Archive

Can those be used for the WinSCP script or should I start fresh?


I found this example and modified it, is it all necessary/correct?

[Reflection.Assembly]::LoadFrom("WinSCPnet.dll") | Out-Null
 
   
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.Protocol = [WinSCP.Protocol]::ftp
    $sessionOptions.HostName = "<IP address>"
    $sessionOptions.UserName = "<username>"
    $sessionOptions.Password = "<password>"
    $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
 
    $session = New-Object WinSCP.Session
 
        $session.Open($sessionOptions)
 
        $localPath = "C:\Sales\"
        $remotePath = "/Gibraltar/Outbox"
        $backupPath = "C:\Sales\Archive"
 
        $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 $_.Exception.Message
    exit 1
}

Thanks,

G.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,468
Location:
Prague, Czechia

Re: Newbie needing some help moving from Filezilla...

First: The existing script is no way Filezilla script. Filezilla does not support scripting at all. The script seems like for Windows built-in command-line FTP client.

Second: The reason the exiting script is short comparing your your new is, that the new "script" is not really a WinSCP script, but full Powershell application that uses WinSCP .NET assembly. Although it is definitely a solution, for such a simple tasks, you can do with simple WinSCP scripting that would involve as little lines as the original script:
https://winscp.net/eng/docs/guide_automation

To finally answer your question: It would be better if you describe any issues you may have. Note that we do not provide code review service here.

Anyway, I can see two issues:
- You are missing slash after /Gibraltar/Outbox
- You do not set an ascii mode (WinSCP defaults to binary mode).

Reply with quote

Advertisement

You can post new topics in this forum