title: I need to modify my WinSCP script to only download files of specific file extensions
body: <p>I have a script that calls the WinSCP .NET assembly. The script downloads the most recent files from an FTP directory and names them based on the their file extension + <code>.txt</code> (<code>2245.xml</code> -> <code>xml.txt</code>).</p>
  
  <p>I need to create a filter to only download file extensions named <code>tn*</code> or <code>nc1</code>. Can anyone point me in the right direction:</p>
  
  <pre><code>$session = New-Object WinSCP.Session
  
  # Connect
  $session.Open($sessionOptions)
  
  # Get list of files in the directory
  $directoryInfo = $session.ListDirectory($remotePath)
  
  # Select the most recent file
  $latest = $directoryInfo.Files |
      Where-Object { -Not $_.IsDirectory} | 
      Group-Object { [System.IO.Path]::GetExtension($_.Name) } | 
      ForEach-Object{ 
          $_.Group | Sort-Object LastWriteTime -Descending | Select -First 1
      }
  
  $extension = [System.IO.Path]::GetExtension($latest.Name)
  "GetExtension('{0}') returns '{1}'" -f $fileName, $extension
  
  if ($latest -eq $Null)
  {
      Write-Host "No file found"
      exit 1
  }
  
  # Download
  
  $latest | ForEach-Object {
      $extension = ([System.IO.Path]::GetExtension($_.Name)).Trim(".")
      $session.GetFiles($session.EscapeFileMask($remotePath + $_.Name), "$localPath\$extension.txt" ).Check()
  }
  </code></pre>
  
  <p>I tried adding a filter in the directory sorting but that didn't work:</p>
  
  <pre><code>    Where-Object { -Not $_.IsDirectory -or [System.IO.Path]::GetExtension($_.Name) -like "tn*" -or [System.IO.Path]::GetExtension($_.Name) -eq "nc1"} | 
  </code></pre>
  
  <p>Thanks!</p>