Zettelkasten / Markdown

As I move more and more to a purely Markdown-based workflow for keeping notes (Zettelkasten ruins my mind), I was looking for a simpler way to create fresh empty markdown files in a folder. Up till now, I right-clicked in Windows Explorer, selected 'New Text file', glanced at the system clock, converted the current time to something like YYYYMMDDhhmmss, and named the text-file accordingly, like 20210108180300.md.

It struck me that this is precisely what I certainly should automate, so I did the following: I created a small Windows batch file named new-markdown.cmd, which I stored in my personal bin directory (C:\Users\chgeuer\bin\new-markdown.cmd):

Contents of new-markdown.cmd

@ECHO OFF

:: https://stackoverflow.com/questions/12635541/safe-way-to-get-current-day-month-and-year-in-batch

:: Check WMIC is available
WMIC.EXE Alias /? >NUL 2>&1 || GOTO s_error

:: Use WMIC to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
   IF "%%~L"=="" goto s_done
      Set _yyyy=%%L
      Set _mm=00%%J
      Set _dd=00%%G
      Set _hour=00%%H
      SET _minute=00%%I
      SET _second=00%%K
)
:s_done

:: Pad digits with leading zeros
      Set _mm=%_mm:~-2%
      Set _dd=%_dd:~-2%
      Set _hour=%_hour:~-2%
      Set _minute=%_minute:~-2%
      Set _second=%_second:~-2%

:: Display the date/time in ISO 8601 format:
SET _isodate=%_yyyy%%_mm%%_dd%%_hour%%_minute%%_second%
cd /D %1
set HEAD=# %_yyyy%-%_mm%-%_dd% %_hour%:%_minute%:%_second%
echo %HEAD% > "%_isodate%.md"
explorer.exe "%_isodate%.md"

Grab some ICO and hook the batch to the registry

Then, I downloaded some ICO file representing markdown, and stored it alongside the batch file, in the bin folder. As a last step, I created these registry entries:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\NewMarkdown]
@="Add Markdown file"
"Icon"="C:\\Users\\chgeuer\\bin\\markdown_106519.ico"

[HKEY_CLASSES_ROOT\Directory\Background\shell\NewMarkdown\command]
@="\"C:\\Users\\chgeuer\\bin\\new-markdown.cmd\" \"%V\""

As a result, you get this entry, when you right-click on the empty space in a folder

The last thing in the batch file (this explorer.exe "%_isodate%.md" thing) kicks off your favorite Markdown editor on the newly created MD file.

Alternatively, you can run this utility (with admin rights): install_markdown_YYYYmmdd.cmd.

@echo off

set ICO=https://icon-icons.com/downloadimage.php?id=160764^&root=2648/ICO/128/^&file=dev_markdown_icon_160764.ico
cmd.exe /c powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -Command "(New-Object System.Net.WebClient).DownloadFile('%ICO%','%USERPROFILE%\bin\markdown.ico')"

set Name=NewMarkdown
SETLOCAL EnableDelayedExpansion
reg.exe ADD HKCR\Directory\Background\shell\%Name%         /f         /t REG_SZ /d "Add Markdown file"
reg.exe ADD HKCR\Directory\Background\shell\%Name%         /f /v Icon /t REG_SZ /d "%USERPROFILE%\bin\markdown.ico"
reg.exe ADD HKCR\Directory\Background\shell\%Name%\command /f         /t REG_SZ /d "\"^%USERPROFILE%\bin\new-markdown.cmd\" \"^%V\""
reg.exe ADD HKCR\Directory\Background\shell\%Name%\command /f         /t REG_SZ /d "\"^%USERPROFILE%\bin\nircmd.exe\" exec hide \"^%USERPROFILE%\bin\new-markdown.cmd\" \"^%V\""

for /f "delims=:" %%a in ('findstr -n "^___DATA___" %0') do set "Line=%%a"
(for /f "skip=%Line% tokens=* eol=_" %%a in ('type %0') do echo(%%a) > "%USERPROFILE%\bin\new-markdown.cmd"
goto:EOF

___DATA___
@ECHO OFF

cd /D %1

:: https://stackoverflow.com/questions/12635541/safe-way-to-get-current-day-month-and-year-in-batch

:: Check WMIC is available
WMIC.EXE Alias /? >NUL 2>&1 || GOTO s_error

:: Use WMIC to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
   IF "%%~L"=="" goto s_done
      Set _yyyy=%%L
      Set _mm=00%%J
      Set _dd=00%%G
      Set _hour=00%%H
      SET _minute=00%%I
      SET _second=00%%K
)
:s_done

:: Pad digits with leading zeros
      Set _mm=%_mm:~-2%
      Set _dd=%_dd:~-2%
      Set _hour=%_hour:~-2%
      Set _minute=%_minute:~-2%
      Set _second=%_second:~-2%

SET _isodate=%_yyyy%%_mm%%_dd%%_hour%%_minute%%_second%

SET HEAD=# %_yyyy%-%_mm%-%_dd% %_hour%:%_minute%:%_second%

echo %HEAD% > "%_isodate%.md"
echo, >>  "%_isodate%.md"
echo, >>  "%_isodate%.md"
echo, >>  "%_isodate%.md"

explorer.exe  "%_isodate%.md"

Last updated