batch-filemarkdownobsidian

How do I modify this BAT code to work for all subfolders?


I need to apply a tag to all .md files in a directory (for use in Obsidian), including all .md files in subfolders. Building off the code in this post, I was able to get it to work for .md files in the same folder as the BAT file with this code:

for %%f in (*.md) do (        
      echo #unpolished >>%%f
)

If someone could modify it to apply to all subfolders, I would be grateful.

Though ideally, if it's not too difficult, could the code be adjusted to do these two things:

  1. Write to the beginning of the .md file.
  2. Instead of the #unpolished tag, could it add this code:
---
tags: unpolished
aliases:
cssclass:
---

The second part is not as significant if too difficult, and it's not timely if someone could add it later. I've tried various batch commands, VBA code, and Notebook++ but I have limited experience and have been unable to get it to work. Thanks for your help.


Solution

  • Thanks all for your feedback. Redditor was able to provide a succinct code solution.

    This code worked for me:

    @echo off
    setlocal enabledelayedexpansion
    set "folder=your file path here"
    for /r "%folder%" %%f in (*.md) do (
      (echo ---& echo tags: unpolished& echo aliases:& echo cssclass:& echo ---& echo. & type "%%f") > "%%~dpnf.tmp"
      move /y "%%~dpnf.tmp" "%%f" > nul
    )
    

    WARNING - for other users, create a backup of your directory before testing.