-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindMostRecent.ps1
60 lines (52 loc) · 1.25 KB
/
FindMostRecent.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Function GetLatestFileInDir($dir)
{
$mostRecent = $null
foreach ($file in Get-ChildItem $dir)
{
if (Test-Path $file.FullName -PathType Container)
{
if ((Split-Path $file.FullName -leaf) -eq "App_Data")
{
continue
}
# Write-Host "Recursing into $($file.FullName)"
$recursed = GetLatestFileInDir(Get-Item $file.FullName)
if ($mostRecent -eq $null -or $recursed.LastWriteTimeUTC -gt $mostRecent.LastWriteTimeUTC)
{
# Write-Host "New latest file $($recursed.FullName) ($($recursed.LastWriteTimeUTC.ToString('f')))"
$mostRecent = $recursed
}
}
else
{
if ($mostRecent -eq $null -or $file.LastWriteTimeUTC -gt $mostRecent.LastWriteTimeUTC)
{
# Write-Host "New latest file $($file.FullName) ($($recursed.LastWriteTimeUTC.ToString('f')))"
$mostRecent = $file
}
}
}
if ($mostRecent -eq $null)
{
return $null
}
return Get-Item $mostRecent.FullName
}
Function Main()
{
$startDir = $pwd
if ($args.Count -gt 0)
{
$startDir = Get-Item $args
}
$latest = GetLatestFileInDir($startDir)
if ($latest -ne $null)
{
Write-Host "Most recent file is $($latest.FullName), updated $($latest.LastWriteTimeUTC.ToString('f'))"
}
else
{
Write-Host "Error: No file found"
}
}
Main