Skip to content

Commit 16b9048

Browse files
committed
🐛 Fix TheBrain backup files deletion issue
This commit fixes the issue of backup files being clean up by TheBrain program. Here are the changes made to `Resize-TheBrainNotesYouTubeThumbnail.ps1`: - Raised the semantic version number to 2.0.0 - Updated the .DESCRIPTION in the comment-based help - Added `$ErrorActionPreference` to stop execution immediately - Added verbose messages to indicate the operating stages - Introduced a folder called "Backup" in the user's Brain data directory as the file backup location - Revamped the code to create backup files by copying the original file to a path under the Backup folder instead of the source directory - Removed the limits of backup files being kept - Introduced a timestamp-formatted filename to backup files - Refactored to use a variable called `$Filename` instead of hardcoding the filename of Brain Markdown notes ("Notes.md") inline - Used the `-Parent` option to ensure the `Split-Path` cmdlet returns the parent folder of target files
1 parent 1d9d5d9 commit 16b9048

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

theBrain/Resize-TheBrainNotesYouTubeThumbnail.ps1

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
Resize YouTube thumbnails down to 30% in theBrain Notes
44
55
.DESCRIPTION
6-
Append `#$width=30p$` to the image address within the Markdown files
6+
Scan all Markdown files in the user's Brain data directory, and apends `#$width=30p$`
7+
to the image URL of embedded YouTube thumbnails within the Markdown files, and backs
8+
up the original notes to the Backup folder before changing the Markdown file content.
79
810
.PARAMETER None
911
@@ -17,7 +19,7 @@
1719
PS C:\> .\Resize-TheBrainNotesYouTubeThumbnail.ps1
1820
1921
.NOTES
20-
Version: 1.1.5
22+
Version: 2.0.0
2123
Author: chriskyfung
2224
License: GNU GPLv3 license
2325
Original from: https://gist.github.com/chriskyfung/ff65df9a60a7a544ff12aa8f810d728a/
@@ -28,44 +30,38 @@
2830
# Enable Verbose output
2931
[CmdletBinding()]
3032

33+
$ErrorActionPreference = "Stop"
34+
3135
# Look up the Notes.md files that locate under the Brain data folder and contain the YouTube thumbnail URLs.
3236
$BrainFolder = . "$PSScriptRoot\Get-TheBrainDataDirectory.ps1"
33-
$MatchInfo = Get-ChildItem -Path $BrainFolder -Filter 'Notes.md' -Recurse | Select-String '\/(hq|maxres)default.jpg\)' -List
37+
$BackupFolder = Join-Path $BrainFolder 'Backup'
38+
39+
$Filename = 'Notes.md'
40+
$FilenameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($Filename)
41+
$FileExtension = [System.IO.Path]::GetExtension($Filename)
42+
43+
Write-Host 'Scanning YouTube thumbnail URLs in Brain Notes...'
44+
$MatchInfo = Get-ChildItem -Path $BrainFolder -Exclude $BackupFolder -Filter $Filename -Recurse | Select-String '\/(hq|maxres)default.jpg\)' -List
3445

3546
# For each matching result
47+
Write-Information 'Backing up and modifying Brain Notes...'
3648
ForEach ($Match in $MatchInfo) {
3749
$FilePath = $Match.Path | Convert-Path # FilePath of the Notes.md file
38-
$ParentFolder = Split-Path -Path $FilePath # Path of the parent folder
39-
# Check if any backup files exist in the parent folder
40-
$NewIndex = 1
41-
if (Test-Path -Path ( -join ($FilePath, '.bak*')) -PathType leaf) {
42-
# If true then determine the index of the last backup file
43-
$LastIndex = (Get-ChildItem -Path $ParentFolder -Filter '*.bak*' | Select-Object Extension -Unique |
44-
Sort-Object -Property Extension | Select-Object -Last 1)[0] -replace ('\D*', '')
45-
$NewIndex = [int]$LastIndex
46-
# If there are more than three backup files
47-
if ($NewIndex -ge 3) {
48-
# Remove the first backup file and re-index the rest of the backup files
49-
Remove-Item ( -join ($FilePath, '.bak1'))
50-
Rename-Item ( -join ($FilePath, '.bak2')) -NewName ( -join ($FilePath, '.bak1'))
51-
Rename-Item ( -join ($FilePath, '.bak3')) -NewName ( -join ($FilePath, '.bak2'))
52-
}
53-
else {
54-
# Else increment the index for the new backup file
55-
$NewIndex++
56-
}
57-
}
58-
$NewName = -join ($FilePath, '.bak', $NewIndex) # FilePath of the new backup file
59-
Copy-Item $FilePath -Destination $NewName # Backup the Notes.md file
60-
Write-Verbose "Created -->' $NewName"
50+
$ParentFolder = Split-Path -Path $FilePath -Parent # Path of the parent folder
51+
$Timestamp = (Get-Item $FilePath).LastWriteTime.ToString('yyyyMMdd_HHmmss')
52+
$BackupFilename = "$FilenameWithoutExtension-$Timestamp$FileExtension~"
53+
$BackupPath = Join-Path $ParentFolder.Replace($BrainFolder, $BackupFolder) $BackupFilename
54+
# Backup the Notes.md file
55+
Copy-Item -Path $FilePath -Destination (New-Item -ItemType File -Force -Path $BackupPath) -Force
56+
Write-Verbose "Created --> '$BackupPath'"
6157
# Amend the link of the YouTube thumbnail with UTF8 encoding
6258
$Pattern = $Match.Matches.Value
6359
$NewString = $Pattern.Replace(')', '#$width=30p$)')
6460
(Get-Content $FilePath -Encoding UTF8).Replace($Pattern, $NewString) | Set-Content $FilePath -Encoding UTF8
65-
Write-Verbose "Modified -->' $FilePath"
61+
Write-Verbose "Modified --> '$FilePath'"
6662
}
6763

68-
Write-Host $MatchInfo.Length 'file(s) found' # Output the number of files found
64+
Write-Host 'Finished: ' $MatchInfo.Length 'file(s) found' # Output the number of files found
6965

7066
$MatchInfo | Format-Table Path | Out-Host # Output the path of the files found
7167

0 commit comments

Comments
 (0)