Home > Back-end >  Using Where-Object to find files created within time range
Using Where-Object to find files created within time range

Time:12-02

all. I'm trying to take the creation date of a file in one folder and use it to filter files in another while also making sure they contain the phrase 'MS'. This is what I have so far:

$MSdat_time = $MSdat_file.CreationTime


# Defining maximum and minimum auto save creation times with a window of  /- 5 min
$auto_maxtime = ($MSdat_time).AddMinutes(5)
$auto_mintime = ($MSdat_time).AddMinutes(-5)

# Locating any Auto Save files created within time frame using 'MS' pattern as a parameter in case of multiple files
$autsav_file = Get-ChildItem  "\\IP.Address\Test Data\Auto Saves" | `
  Where-Object {($_.LastWriteTime -ge $auto_mintime) -and ($_.LastWriteTime -le $auto_maxtime)} | `
  Select-String -Pattern 'MS' | Select-Object -Unique Path

I put in 'IP address' as a place holder. So far, it's returning nothing even though I know a file with those parameters exists and this section of code was working fine yesterday.

CodePudding user response:

Check that your $MSdat_file variable has a value. It could be that your previous PS sessions had given that variable a value outside of your script.

After assigning $MSdat_file = Get-Item "./out.wav", I was able to get expected output:

PS /Users/ethansmith> /Users/ethansmith/Documents/test.ps1
PS /Users/ethansmith> $autsav_file

Path
----
/Users/ethansmith/Documents/test.ps1

CodePudding user response:

Thank you everyone for your help! @ethan was right, the issue was with my $MSdat_file variable having an old value saved to it. Should have traced my code back to the beginning sooner haha. Thanks everyone!

  • Related