Hey there, I’ve been importing and cleaning up some notes from google keep, and a lot of them came over with aliases.
I wanted to remove these with powershell, but I’ve been running into some problems, here’s the script I wrote:
# Define the pattern to match files - "*.md" for Markdown files
$filePattern = "*.md"
# Iterate over all .md files in the directory and its subdirectories
Get-ChildItem -Path . -Filter $filePattern -Recurse | ForEach-Object {
$filePath = $_.FullName
# Read the content of the file
$fileContent = Get-Content -Path $filePath -Raw
# Define a regex pattern to match the 'aliases' property and any multiline content until the next property or end of the file
$pattern = '(?ms)\naliases:.*?(?=\r?\n\S|\Z)'
# Check if the file content matches the pattern
if ($fileContent -match $pattern) {
# Replace the 'aliases' property and its content with nothing
$newContent = $fileContent -replace $pattern, ''
# Write the modified content back to the file ONLY if there was a match
# Using [System.IO.File]::WriteAllText to ensure UTF-8 encoding without BOM
$utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $false
[System.IO.File]::WriteAllText($filePath, $newContent, $utf8NoBomEncoding)
}
}
Write-Host "Files containing 'aliases' have been processed."
I was testing out a few format options in powershell because I noticed two things
- obsidian was not recognizing the yaml properties immedietely after the running this script, if I modified the file in the obsidian editor then the properties would return.
- when I tried to backup with git, the diff shows that each line in the file has changed, ie.
----
-aliases:
-tags:
- - questions
- - dynamo
----
-Is refinery an evolutionary solver?
-
-Van wijnen project
+---
+tags:
+ - questions
+ - dynamo
+---
+Is refinery an evolutionary solver?
+
+Van wijnen project
Whether I used no explicit encoding, UTF8, or UTF8NoBomEncoding, I get these two persistent problems. Does anyone have any idea how to clean this up, or if I’m even on the right track here?
Cheers
Dan