ps/Modules/Cole.PowerShell.Developer/Public/Test-WasFileModifiedWithin.ps1
2023-05-30 22:51:22 -07:00

51 lines
1.3 KiB
PowerShell

function Test-WasFileModifiedWithin {
<#
.SYNOPSIS
Was this file modified within the last X period of time
.PARAMETER Path
The path of the file
.PARAMETER Last
Period of time to compare against
.PARAMETER Count
The number of the time period
.EXAMPLE
Test was this file modified in the past month
Test-WasFileModifiedWithin -Last Month -Path $somePath
.EXAMPLE
Test was this file modified in the past two weeks
Test-WasFileModifiedWithin -Last Week -Count 2 -Path $somePath
.OUTPUTS
Will return false if the path does not exist
#>
param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Path,
[Parameter(Mandatory = $true)]
[ValidateSet('Month','Week','Day','Hour')]
[string]$Last,
[Parameter(Mandatory = $false)]
[int]$Count = 1
)
if (!(Test-Path -Path $Path)) {
return $false
}
$targetDateTime = [System.DateTime]::Now
$targetDateTime = switch ($Last) {
'Month' { $targetDateTime.AddMonths(-1 * $Count) }
'Week' { $targetDateTime.AddDays(-1 * 7 * $Count) }
'Day' { $targetDateTime.AddDays(-1 * $Count) }
'Hour' { $targetDateTime.AddHours(-1 * $Count) }
}
return ((Get-Item -Path $Path).LastWriteTime -lt $targetDateTime)
}