Search code examples
powershellftpwinscpwinscp-net

Copy files to FTP and archive with today's date


I need to create a script that does the following:

  1. Copies all files in a folder to an FTP site.
  2. If the copy was successful move the files to an archive.
  3. The archive should be a freshly created folder with today's date (so we know when they were transmitted).

I've tried to cannibalise other scripts to get something to work but I'm not getting anywhere so I need some help I've been working on this for hours.

I'm using the WinSCP DLL only because my other (working) script uses SFTP which needs it. I know normal FTP doesn't but I couldn't find any easily transferrable code so trying to modify that instead.

So, here's the code I have, which doesn't even run, never mind run properly, can someone help me get it right? Sorry it's a bit of a mess.

param (
    $localPath = "c:\test\source",
    $remotePath = "/upload",
    $folder = ($_.CreationTime | Get-Date -Format yyyyMMdd)
    # not sure this works but don't see how to point the destination
    # to the newly created folder
    $backupPath = "c:\test\destination\$folder"  
)

try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\Windows\System32\WindowsPowerShell\v1.0\WinSCPnet.dll"

    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::ftp
        HostName = "xxxxxxxx"
        UserName = "xxxxxxxx"
        Password = "xxxxxxxx"
    }

    $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)
            {
                # If today's folder doesn't exist, create it
                if (!(Test-Path $BackupPath))
                {
                    New-Item -ItemType Directory -Force -Path $BackupPath
                }

                Write-Host ("Upload of {0} succeeded, moving to Uploaded folder" -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
}

So there's the code. I'm happy to use built in PowerShell for the FTP side to simplify it, I just want it to work.


Solution

  • I'm not sure what's your concern with the code. It looks pretty much ok, except for a syntax error, when setting $folder:

    Why are you even trying to use $_.CreationTime as folder timestamp? Just use current date:

    $folder = (Get-Date -Format "yyyyMMdd")
    

    See Formatting timestamps in PowerShell in WinSCP documentation.

    Also I do not see a point of setting $folder and $backupPath in the params block. Move it after the params block. If you want this anyway, you are missing a comma after the $folder assignment.


    Other than that, your code should work.

    You cannot simplify it by using the built-in PowerShell (or rather .NET) FTP functionality, as it does not have as powerful commands as WinSCP .NET assembly.


    I'd write the code as:

    $localPath = "C:\source\local\path\*"
    $remotePath = "/dest/remote/path/"
    $folder = (Get-Date -Format "yyyyMMdd")
    $backupPath = "C:\local\backup\path\$folder"
    
    # If today's folder doesn't exist, create it
    if (!(Test-Path $BackupPath))
    {
        New-Item -ItemType Directory -Force -Path $BackupPath | Out-Null
    }
    
    try
    {
        # Load WinSCP .NET assembly
        Add-Type -Path "WinSCPnet.dll"
    
        # Setup session options
        $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
            Protocol = [WinSCP.Protocol]::ftp
            HostName = "ftp.example.com"
            UserName = "username"
            Password = "password"
        }
    
        $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
        {
            # Disconnect, clean up
            $session.Dispose()
        }
    
        exit 0
    }
    catch [Exception]
    {
        Write-Host "Error: $($_.Exception.Message)"
        exit 1
    }
    

    Based on Moving local files to different location after successful upload.