PowerShell example for downloading latest file

Advertisement

freff
Joined:
Posts:
2
Location:
Uk

PowerShell example for downloading latest file

Hi,

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:
https://winscp.net/eng/docs/script_download_most_recent_file#powershell

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

Regards
Steve

Reply with quote

Advertisement

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

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.

Reply with quote

freff
Joined:
Posts:
2
Location:
Uk

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
Regards
Steve

Reply with quote

justadakaje
Joined:
Posts:
2
Location:
Dallas, TX, USA

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 https://winscp.net/eng/docs/script_download_most_recent_file. 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.

Reply with quote

justadakaje
Joined:
Posts:
2
Location:
Dallas, TX, USA

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
$session.EnumerateRemoteFiles
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!

Reply with quote

Advertisement

martin
Site Admin
martin avatar

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.

Reply with quote

Advertisement

You can post new topics in this forum