Post a reply

Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)


Topic review


Re: Found a solution: use $session.EnumerateRemoteFiles

@justadakaje: If you need to search for the latest file recursively, then indeed the Session.EnumerateRemoteFiles is the right way.

Found a solution: use $session.EnumerateRemoteFiles

In my case, the FTP Site consists of subdirectories of a timestamp and the files I need to download are inside those. So, rather than doing a
$directoryInfo = $session.ListDirectory($remotePath)

which now I understand would be useful if all the files I'm looking for are in a single remote directory, I'm doing a
where I'm specifying the remote path, a wildcard, and the EnumerationOptions of AllDirectories so I can be sure to always find the latest file. If there's a cleaner way to do this, I'm all ears. But, in my case, since the remote directory contains a collection of subdirectories, the ListDirectory method wasn't returning the collection of files. Hope this helps someone else! Cheers!

Where clause to filter out directories not working?

Hi @martin
For me, the PowerShell Where-Object clause to filter out directories from the Files collection does not seem to be working, as intended. I'm essentially using your whole script example from I can manually step through the script, line by line and when I execute the following, a single file is never selected...
# Select the most recent file
$latest =
    $directoryInfo.Files |
    Where-Object { -Not $_.IsDirectory } |
    Sort-Object LastWriteTime -Descending |
    Select-Object -First 1

Could this just be a problem with the FTP Server to which I'm connecting? I know there are files present because I can manually open WinSCP client and see them. I'm just trying to automate the process to download the latest file.

Re: Powershell example for downloading latest file

Hi @Martin,

When I run the PowerShell script it does not create a log file

After looking at the error I can see that on the remote path I am missing the / at the end I have added the / for the path and it appears to be working now.

Many thanks for your help

Re: Powershell example for downloading latest file

First, thanks for pointing out the problem with missing $.

Regarding the error: What is /usr/xxx/xxx.cvs? Is it a path you set $remotePath to? The purpose of the script is to find the latest file in a directory. So the $remotePath should be set to a path to the directory, not a specific path.

Posting a complete error message, or even better a log file, would help.

PowerShell example for downloading latest file


I am new to scripting and opted for the PowerShell example purely because I have PowerShell on my laptop.

In the example that is supplied on the website I am having issue with the code and suspect that it is related to me not the example.

Here is the example that you have supplied:

My first thought it was going to be straight forward, however had an issue registering winscpnet.dll then an issue with the script DirectoryInfo was not a cmd, I guess that this line needed a $ before the command.

After inserting a $, the script now moves onto the next issue.
Can't get attributes of file '/usr/xxx/xxx.cvs'

Can some one please point me in the right direction I have ensured that I have two files in the folder and while they have the same date stamp they do have different time stamps.

Any help would be great