Download most recent file of specific file name pattern from SFTP server using WinSCP in PowerShell

Viewed 807

I have to copy the most recent file with file mask FileName_A_* from SFTP location and place them in an sharedrive.

I tried using WinSCP. I have created an Mostrecent.txt file with below code and placed it under C:\Program Files (x86)\WinSCP. Another batch file Mostrecent.bat to execute the script from HourlyFile.txt

Mostrecent.txt

option batch abort
option confirm off
open sftp..........
$source = '/outbound/test'
$destination = '\\sharedrive\'
@(Get-ChildItem $source -Filter FileName_A_* | Sort LastWriteTime -Descending)[0] | % { Copy-Item -path $_.FullName -destination -force} 
exit

Mostrecent.txt

option batch abort
option confirm off
open sftp..........
$dir= '/outbound/test/FileName_A_*'
get  Dir | select -last 1 \\sharedrive
exit

The SFTP location will contain different files with different file name and extension. I need to just copy the most recent file with file pattern FileName_A_*. The file name will be:

FileName_A_20190619100000.txt
FileName_A_20190619110007.txt
FileName_A_20190619120040.txt
FileName_A_20190619130100.txt
1 Answers
Related