Akanksha Gahlod
Akanksha Gahlod

Reputation: 11

Check if file has arrived in particular range of time using powershell

I want to check if file has been updated in a particular time of range like 9:00 PM to 11:00 PM. If true the print "File arrived" if not then print "NOt updated", using powershell.

what I tried is this :

$file="F:\akku.txt"    
$a=(Get-Item $file).LastWriteTime    
$b= $a.ToShortTimeString()    
$startTime= Get-Date "09:00 PM"     
$endTime=Get-Date "11:00 PM"    
$b    
$startTime -gt $b     
$endTime -lt $b    
if(  $b -gt $startTime.TimeOfDay  -and $b -lt $endTime.TimeOfDay )
{
    Write-Host "Replication received"
} else{
    Write-Host "Replication not received"
}

Kindly let me know if there is some other way of detecting the file

Upvotes: 0

Views: 1736

Answers (3)

No Refunds No Returns
No Refunds No Returns

Reputation: 8346

Simpler is better?

$status = "NotFound " ; gci foo.txt | ? { $_.LastWriteTime.Hour -ge 21 -and $_.LastWriteTime.Hour -lt 23 } | select -First 1 | % { $Status = "Found"}
Write-Host $Status

An actual time range:

$end = Get-Date -Year 2019 -Month 1 -Day 1 -Hour 0 -Minute 0 -Second 0 -Millisecond 0
$start = Get-Date -Year 2018 -Month 1 -Day 1 -Hour 0 -Minute 0 -Second 0 -Millisecond 0
gci foo.txt | ? { $_.LastWriteTime -ge $start -and $_.LastWriteTime -le $end }

Either can be parameterized into a script.

If your file may not exist you will want to add try/catch around this but your example didn't include it. I'll assume you simplified for posting.

Upvotes: 0

Lee_Dailey
Lee_Dailey

Reputation: 7489

this takes a slightly different approach. it checks the hour-of-the-day to see if it's in the desired range using a standard number range.

it also presumes you actually want the dates from yesterday since testing for today BEFORE the test range makes no sense ... and testing between 2300 & 2359 seems rather unlikely. [grin]

# get date with time set @ midnite
$Yesterday = (Get-Date).AddDays(-1).Date
# 9pm = 2100 hours
$UpdateHourRange = 21..23

# comment out the one you DO NOT want to test with [*grin*]

<#
# create a file with the LastWriteTime set to yesterday @ 2200
$FileName = 'C:\temp\IN_Timespan.txt'
$Null = New-Item -Path $FileName -ItemType File -Force
$TargetFile = Get-Item -LiteralPath $FileName
$TargetFile.LastWriteTime = Get-Date -Day ($Yesterday.Day) -Hour 22 -Minute 00 -Second 00
#>

#<#
# create a file with LastWriteTime set to "now"
$FileName = 'C:\temp\OUT_Timespan.txt'
$Null = New-Item -Path $FileName -ItemType File -Force
#>

#$FileName = "$Env:TEMP\Enable1_WordList_File.txt"
$TargetFile = Get-Item -LiteralPath $FileName

if ($TargetFile.LastWriteTime.Date -eq $Yesterday -and
    $TargetFile.LastWriteTime.Hour -in $UpdateHourRange)
    {
    'Update was in the required timespan.'
    '    LastWriteTime = {0}' -f $TargetFile.LastWriteTime
    '             Hour = {0}' -f $TargetFile.LastWriteTime.Hour
    }
    else
    {
    Write-Warning 'The file was NOT updated in the required timespan.'
    Write-Warning ('    LastWriteTime = {0}' -f $TargetFile.LastWriteTime)
    Write-Warning ('             Hour = {0}' -f $TargetFile.LastWriteTime.Hour)
    }

output for the "now" file ...

WARNING: The file was NOT updated in the required timespan.
WARNING:     LastWriteTime = 2018-11-18 3:50:19 PM
WARNING:              Hour = 15

Upvotes: 1

Drew
Drew

Reputation: 4030

A few things with your current code.

The $b and the following two lines are useless, unsure what you were doing there.

You don't need the short time string as PowerShell will treat them as a DateTime object so you can use the full time. E.G. Monday, 19 November 2018 7:53:48 AM. As such, no need for .TimeOfDay.

$File = "F:\akku.txt"
$GetFile = (Get-Item $file).LastWriteTime
$StartTime = Get-Date "09:00 PM"
$EndTime = Get-Date "11:00 PM"
if($GetFile -gt $StartTime -and $GetFile -lt $EndTime){
    Write-Host "Replication received"
}else{
    Write-Host "Replication not received"
}

Upvotes: 3

Related Questions