1

I'm using a PowerShell script to retrieve a file from a remote directory. I only want to retrieve a file if it was modified within the last hour. I was able to get the most recent file using the following code:

$directoryInfo = $session.ListDirectory($remotePath) 

$latest = 
    $directoryInfo.Files | 
    Where-Object { -Not $_.IsDirectory } | 
    Sort-Object LastWriteTime -Descending | 
    Select-Object -First 1 

I believe that I need to add another condition to the Where-Object clause, but I don't know the proper format. For example,

    Where-Object { -Not $_.IsDirectory and <created/modified within the last hour> } 

How do I do this? Is there a better/simpler way?

2 Answers 2

1

Extend you current where-block to check if LastWriteTime is greater (newer) than a datetime-object representing the previous hour. Ex:

$lasthour = (Get-Date).AddHours(-1)

$directoryInfo = $session.ListDirectory($remotePath) 

$latest = $directoryInfo.Files | 
Where-Object { (-Not $_.IsDirectory) -and ($_.LastWriteTime -gt $lasthour) } | 
Sort-Object LastWriteTime -Descending |
Select-Object -First 1 
Sign up to request clarification or add additional context in comments.

5 Comments

+1, though not sure if the OP actually wants the Sort-Object LastWriteTime -Descending | Select-Object -First 1 parts. I can image the intention is to download all recent files, not just the most recent file.
Yeah, but that wasn't the question. I removed them at first, but since I don't know what he wants to do I added them back in to only fix what he was missing. :-)
The title says "retrieve files modified within the last hour".
The title isn't the question... The question is how he can filter out old items using the where-statement he has partly written. Either way, it takes him 2 seconds to remove the lines. :-)
Also, the question itself actually says he only wants to retrieve "a file". You're probably right, but since the question/title is unclear (mismatch), I decided to answer what he wanted help with and nothing more. We try to avoid writing complete solutions for others as this is supposed to be reusable for others who may not want to download the files, but rather just find the new items. :-)
0

If you want to download all files created/modified within the last hour, use:

$directoryInfo = $session.ListDirectory($remotePath) 

$limit = (Get-Date).AddHours(-1)

$recentFiles = 
    $directoryInfo.Files | 
    Where-Object { (-Not $_.IsDirectory) -And ($_.LastWriteTime -Gt $limit) }

foreach ($fileInfo in $recentFiles)
{
    $sourcePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName)
    $session.GetFiles($sourcePath, $localPath + "\*").Check()
}

Some official WinSCP .NET assembly examples used to make the code:

3 Comments

I see now that my question and topic are somewhat at odds with one another. My apologies for the confusion. Here is more background:
Background: We have a process that places one file in the source folder each weekday around 5:20pm. I need to retrieve that file, rename it and put it to another server. The script is scheduled to run on weekdays. My original code worked fine except on Federal Holidays, when no file is created. As a result, the previous days' file would get reprocessed. Thus, I switched to only getting files modified within the last hour rather than getting the most recent file.
Frode F.'s solution worked as desired. Thanks for all of the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.