Last active
November 22, 2023 01:22
-
-
Save chriskyfung/ff65df9a60a7a544ff12aa8f810d728a to your computer and use it in GitHub Desktop.
A PowerShell script to resize all YouTube thumbnails down to 30% in your theBrain Notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Resize YouTube thumbnails down to 30% in theBrain Notes | |
.DESCRIPTION | |
Append `#$width=30p$` to the image address within the Markdown files | |
.PARAMETER None | |
.INPUTS | |
None. | |
.OUTPUTS | |
None | |
.EXAMPLE | |
PS C:\> .\Resize-TheBrainNotesYouTubeThumbnail.ps1 | |
.NOTES | |
Version: 1.1.5 | |
Author: chriskyfung | |
Website: https://chriskyfung.github.io | |
Creation Date: 2023-04-28 | |
Last Modified: 2023-11-14 | |
#> | |
# Enable Verbose output | |
[CmdletBinding()] | |
$BrainFolder = -join([Environment]::GetFolderPath('MyDocuments'), '\Brains\') | |
$MatchInfo = Get-ChildItem -Path $BrainFolder -Filter 'Notes.md' -Recurse | Select-String '\/(hq|maxres)default.jpg\)' -List | |
# For each matching result | |
ForEach ($Match in $MatchInfo) { | |
$FilePath = $Match.Path | Convert-Path # FilePath of the Notes.md file | |
$ParentFolder = Split-Path -Path $FilePath # Path of the parent folder | |
# Check if any backup files exist in the parent folder | |
$NewIndex = 1 | |
if (Test-Path -Path ( -join ($FilePath, '.bak*')) -PathType leaf) { | |
# If true then determine the index of the last backup file | |
$LastIndex = (Get-ChildItem -Path $ParentFolder -Filter '*.bak*' | Select-Object Extension -Unique | | |
Sort-Object -Property Extension | Select-Object -Last 1)[0] -replace ('\D*', '') | |
$NewIndex = [int]$LastIndex | |
# If there are more than three backup files | |
if ($NewIndex -ge 3) { | |
# Remove the first backup file and re-index the rest of the backup files | |
Remove-Item ( -join ($FilePath, '.bak1')) | |
Rename-Item ( -join ($FilePath, '.bak2')) -NewName ( -join ($FilePath, '.bak1')) | |
Rename-Item ( -join ($FilePath, '.bak3')) -NewName ( -join ($FilePath, '.bak2')) | |
} | |
else { | |
# Else increment the index for the new backup file | |
$NewIndex++ | |
} | |
} | |
$NewName = -join ($FilePath, '.bak', $NewIndex) # FilePath of the new backup file | |
Copy-Item $FilePath -Destination $NewName # Backup the Notes.md file | |
Write-Verbose "Created -->' $NewName" | |
# Amend the link of the YouTube thumbnail with UTF8 encoding | |
$Pattern = $Match.Matches.Value | |
$NewString = $Pattern.Replace(')', '#$width=30p$)') | |
(Get-Content $FilePath -Encoding UTF8).Replace($Pattern, $NewString) | Set-Content $FilePath -Encoding UTF8 | |
Write-Verbose "Modified -->' $FilePath" | |
} | |
Write-Host $MatchInfo.Length 'file(s) found' # Output the number of files found | |
$MatchInfo | Format-Table Path | Out-Host # Output the path of the files found | |
Return |
1.1.1 Changelog
- Fixed UTF-8 encoding issue
- Fixed issues with creating backup files
The gist has been merged to my GitHub repo, My-PowerShell-Script. Please visit the repository to find the latest version of the script.
Also, check out other PowerShell scripts at the repo as well. I hope you find them useful! 🙂
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1.1.0 Changelog
hqdefault.jpg
rather thanmaxresdefault.jpg
in the image address-Verbose
parameter only