Created
October 4, 2024 08:13
-
-
Save dhcgn/e08b7f515fe860c435d0d79d53d31d1f to your computer and use it in GitHub Desktop.
Removes empty Obsidian Markdown files or files containing a special mark. Place this script in the folder containing the Obsidian files.
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 | |
Removes empty Obsidian Markdown files or files containing a special mark. Place this script in the folder containing the Obsidian files. | |
.DESCRIPTION | |
The script performs the following operations: | |
1. Searches for empty Markdown files in the folder where the script is located and its subdirectories (excluding hidden folders). | |
Prompts the user for confirmation before deleting these files. | |
2. Searches for Markdown files containing a specific line of text, excluding those in a specified 'Templates' folder | |
and hidden folders. Prompts the user for confirmation before deleting these files. | |
#> | |
# Script Folder | |
$rootDir = $PSScriptRoot | |
# | |
# Deleting empty Markdown files | |
# | |
Write-Host "Searching in this folder for empty files: $rootDir" -ForegroundColor Green | |
# Get all .md files in the root directory and its subdirectories which are empty, exclude folders starting with a "." | |
$emptyFiles = Get-ChildItem -Path $rootDir -Recurse -File -Include *.md | Where-Object { $_.Length -eq 0 -and $_.DirectoryName -notmatch '^.*\\\.' } | |
Write-Host "Found $($emptyFiles.Count) empty files." -ForegroundColor Yellow | |
# Group by directories containing the empty files and display count per directory | |
$emptyDirs = $emptyFiles | ForEach-Object { $_.DirectoryName.Replace($rootDir, "") } | |
Write-Host "Ordner Übersicht" | |
$emptyDirs | Group-Object -NoElement | Format-Table -AutoSize Count, Name | |
# Display list of empty files (optional) | |
$emptyFiles | |
# delete the empty files | |
$emptyFiles | Remove-Item -Confirm | |
# | |
# Deleting Markdown files with a specific line | |
# | |
# Files with EmptyMarking and not in folder of Templates, the EmptyMarking is added trough a template for Daily Notes. | |
$EmptyMarking = '🟥🟥🟥 REMOVE THIS LINE 🟥🟥🟥' | |
$TemplatingFolderName = 'Templates' | |
# Get all .md files excluding the 'Templates' folder that contain the empty marking | |
$markedFiles = Get-ChildItem -Path $rootDir -Recurse -File -Include *.md | Where-Object { | |
$_.FullName -notlike "*$TemplatingFolderName*" -and (Get-Content -Path $_.FullName -Raw) -like "*$EmptyMarking*" -and $_.DirectoryName -notmatch '^.*\\\.' | |
} | |
Write-Host "Found $($markedFiles.Count) files with empty marking." -ForegroundColor Yellow | |
# Group by directories containing the marked files and display count per directory | |
$markedDirs = $markedFiles | ForEach-Object { $_.DirectoryName.Replace($rootDir, "") } | |
Write-Host "Ordner Übersicht" | |
$markedDirs | Group-Object -NoElement | Format-Table -AutoSize Count, Name | |
# Display list of files with empty marking (optional) | |
$markedFiles | |
# delete the empty files | |
$markedFiles | Remove-Item -Confirm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment