Topic "Search recursively download most recent file"

Author Message
Partridge

Guest


I want to make a connection to an SFTP server and download the latest zip-file.
The remote path consists of several directories which contains zip-files.
At the moment the script only downloads zip-files from /Output
But I also want to check the subfolders for example /Output/Work1

How can I accomplish this? see code below:


param (
$localPath = "D:\temp\",
$remotePath = "/Output/"
)

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

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "myhost"
UserName = "user"
Password = "password"
SshHostKeyFingerprint = "ssh-rsa 2048 xxx xxx xxx xxx"
}

$session = New-Object WinSCP.Session

try
{
# 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 } |
#$directoryInfo.Files |
#get-childitem -Recurse | Where-Object {$_.Extension -eq ".zip"} |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1

# Any file at all?
if ($latest -eq $Null)
{
Write-Host "No file found"
exit 1
}

# Download the selected file
$session.GetFiles($session.EscapeFileMask($remotePath + $latest.Name), $localPath).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}

exit 0
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
exit 1
}
Advertisements
Partridge

Guest


Anyone?
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24991
Location: Prague, Czechia
Instead of using $session.ListDirectory($remotePath).Files, use $ession.EnumerateRemoteFiles($remotePath, "*", [WinSCP.EnumerationOptions]::AllDirectories) (5.8.3 RC version only)
https://winscp.net/eng/docs/library_session_enumerateremotefiles
Guest




prikryl wrote:
Instead of using $session.ListDirectory($remotePath).Files, use $ession.EnumerateRemoteFiles($remotePath, "*", [WinSCP.EnumerationOptions]::AllDirectories) (5.8.3 RC version only)
https://winscp.net/eng/docs/library_session_enumerateremotefiles


I have changed
# Get list of files in the directory
$directoryInfo = $session.ListDirectory($remotePath)

Into
# Get list of files in the directory
$session.EnumerateRemoteFiles($remotePath, "*", [WinSCP.EnumerationOptions]::AllDirectories)

It does not generate an error anymore but does not download a file either.
There are two files on the remote location.
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24991
Location: Prague, Czechia
Show us your complete code.
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License