-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange-date-mofified.ps1
66 lines (56 loc) · 1.7 KB
/
change-date-mofified.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
61
62
63
64
65
66
$pathToFiles = if ($args.Length -gt 0)
{ $args[0]
} else
{ Get-Location
}
if (-not (Test-Path $pathToFiles))
{
Write-Error "The provided path does not exist."
exit
}
$qmdFiles = Get-ChildItem -Path $pathToFiles -Filter *.qmd -Recurse
foreach ($file in $qmdFiles)
{
$content = Get-Content $file.FullName
$updated = $false
# Get file's date write time, only considering the date part
$fileLastWriteTime = (Get-Item $file.FullName).LastWriteTime.Date
# Attempt to find and parse the last-updated date from the file
$lastUpdatedLine = $content | Where-Object { $_ -match '^date-modified: (.*)$' }
$fileNeedsUpdate = $true
if ($lastUpdatedLine)
{
$lastUpdatedDateStr = $lastUpdatedLine -replace '^date-modified: (.*)$', '$1'
try
{
$lastUpdatedDate = [DateTime]::ParseExact($lastUpdatedDateStr, "yyyy-MM-dd", $null)
if ($fileLastWriteTime -le $lastUpdatedDate)
{
$fileNeedsUpdate = $false
}
} catch
{
Write-Warning "Failed to parse date-modified date for $($file.FullName)"
}
}
if ($fileNeedsUpdate)
{
# Your update logic goes here. Make sure to set $updated = $true if changes are made.
# Example: Updating the last-updated field
$lastUpdatedIndex = [array]::IndexOf($content, $lastUpdatedLine)
if ($lastUpdatedIndex -ne -1)
{
$content[$lastUpdatedIndex] = "date-modified: " + $fileLastWriteTime.ToString("yyyy-MM-dd")
$updated = $true
} else
{
$content += "date-modified: " + $fileLastWriteTime.ToString("yyyy-MM-dd")
$updated = $true
}
if ($updated)
{
$content | Set-Content $file.FullName
Write-Output "Updated file: $($file.FullName)"
}
}
}