Powershell - Download folders from ftp with wildcard

Advertisement

CptDuckie
Joined:
Posts:
3
Location:
Netherlands

Powershell - Download folders from ftp with wildcard

Hi all,

I'm using a Powershell script to auto download updated files from an FTP directory, im using the synchronisation method for this. I want to be able to specify a directory with a wildcard. Like
folder /download/software/version/4*. Or be able to download everything with folder /download/software/version/400 and higher. I want to do this, because there are older version, ex. version 100, 200, 300 which won't need to be downloaded, exact 400 and higher.

Is there any way to realise this? Currently using this script:

# Specify directories
$localPath = "C:\Temp\Software"
$remotePath =  "/Download/Software/Version/"

    # Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP-5.13.4-Automation\WinSCPnet.dll"
 

try
{

    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::ftp
        HostName = "download.ftp.com"
        UserName = "anonymous"
        Password = "anonymous"
    }
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        Write-Host "Connecting to FTP..."
        $session.Open($sessionOptions)
 
        # Download files
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
         
        Write-Host "Synchronizing changes..."
        $transferResult =
            $session.SynchronizeDirectories(
                  [WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $false. $transferOptions)
 
        # Throw on any error
        $transferResult.Check()
 
        # Print results
        foreach ($transfer in $transferResult.Downloads)
        {
            Write-Host "Download of $($transfer.FileName) succeeded"
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

If I add a wildcard to the /Download/Software/4* directory it fails with the following error:

PS C:\temp> C:\Temp\DownloadSoftware.ps1
Connecting to FTP...
Synchronizing changes...
Error: Exception calling "Check" with "0" argument(s): "Error listing directory '/Download/Software/4*'.
Could not retrieve directory listing
The filename, directory name, or volume label syntax is incorrect. "

Reply with quote

Advertisement

CptDuckie
Joined:
Posts:
3
Location:
Netherlands

      # Download files
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
        $transferOptions.FileMask = "| 10*/; 20*/; 30*/; 40*; 410*; 411*"
         
        Write-Host "Synchronizing changes..."
        $transferResult =
            $session.SynchronizeDirectories(
                  [WinSCP.SynchronizationMode]::Local, $DownloadPath, $ExactVersion, $false, $transferOptions)

If I do it like this, it seems to ignore the filemask/excluded files and still downloads everything starting by 10.. Did I configure the filemask or fileoptions wrong?

Reply with quote

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

CptDuckie wrote:

        $transferOptions.FileMask = "| 10*/; 20*/; 30*/; 40*; 410*; 411*"
The above is wrong.
Though even your wrong code does not explain the behavior you are seeing.
Use one of the examples I've suggested. If it does not work, please attach a complete session log file.

Reply with quote

CptDuckie
Joined:
Posts:
3
Location:
Netherlands

I have changed it to

$transferOptions.FileMask = "| 1*/; 2*/; 3*/"
and for example, it starts downloading folder 397. Which should be excluded.

Here is the session.log. I have filtered out a few details.
Description: session logfile

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum