Useful cmd for windows-users. Converter page.md to page/index.md and reverse; slug to filename and etc

All this files should be placed at C:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\SendTo\

And run through the right mouse button, select the folder, right-click and select Send To

Converter slug to filename

Version for YAML Front Matter.

slug2name.cmd

@echo off
pushd "%~1"
for /f %%A in ('dir /a-d/b "*.m*"') do (
    for /f "tokens=1* delims=:" %%B in ('type "%%A"^| findstr /b slug:') do (
        for /f "tokens=*" %%D in ("%%C") do (
            if "%%A" neq "%%D%%~xA" (
               rename %%A %%D%%~xA
               echo %%A =^> %%D%%~xA
            )
        )
    )
)
pause

Converter article.md to article/index.md

file2folder.cmd

pushd "%~1"
for /f "delims=. tokens=1" %%a in ('dir /a-d/b "*.md"') do (
    if not %%~a==index (
      mkdir %%~a
      move /Y %%~a.md %%~a\index.md
    )
)
for /f "delims=. tokens=1" %%a in ('dir /a-d/b "*.mmark"') do (
    if not %%~a==index (
      mkdir %%~a
      move /Y %%~a.mmark %%~a\index.mmark
    )
)
pause

Converter article/index.md to article.md

folder2file.cmd

pushd "%~1"
for /f "tokens=*" %%A in ('dir /ad/b') do (
    if exist .\%%A\index.md (
      move /Y .\%%A\index.md %%A.md
      rmdir %%A
    )
)
for /f "tokens=*" %%A in ('dir /ad/b') do (
    if exist .\%%A\index.mmark (
      move /Y .\%%A\index.mmark %%A.mmark
      rmdir %%A
    )
)
pause

Generator index.md for file structure like article.md

Version for YAML Front Matter.

dir2list.cmd

pushd "%~1"
if exist index.md (
   rename  index.md index.%date:.=%.bak
)
for /f %%A in ('dir /a-d/b "*.m*"') do (
    for /f "tokens=1* delims=:" %%B in ('type "%%A"^| findstr /b title:') do (
        for /f "tokens=*" %%D in ("%%C") do (
            for /f "delims='" %%E in ("%%~D") do (
                echo - [%%E](%%~nA/^)>>index.md
                echo.>>index.md
            )
        )
    )
)
pause

Generator index.md for file structure like article/index.md

Version for YAML Front Matter.

subdir2list.cmd

pushd "%~1"
if exist index.md (
   rename  index.md index.%date:.=%.bak
)
for /f %%A in ('dir /ad/b') do (
    for /f "tokens=1* delims=:" %%B in ('type ".\%%A\index.md" ^|findstr /B title:') do (
        for /f "tokens=*" %%D in ("%%C") do (
            for /f "delims='" %%E in ("%%~D") do (
                echo - [%%E](%%~nA/^)>>index.md
                echo.>>index.md
            )
        )
    )
)
pause

1 Like

Adds the common Front Matter to all files in the folder.

Text of the Front Matter should be placed in a separate file ‘frontmatter’ with any extension in the same folder, but the ‘frontmatter’ extension must not start with the letter ‘m’ (.yaml, .toml and etc. looks good). After execution the script generates archives of source files with the extension .bak, which must be removed by hand if the execution was successful.

addfrontmatter.cmd

pushd "%~1"
if exist frontmatter.* (
      for /f "delims=*" %%A in ('dir /a-d/b "*.m*"') do (
            del /q %temp%\%%A.frontmatter
            copy frontmatter.*+%%A %temp%\%%A.frontmatter
            rename %%A %%A.bak
            move %temp%\%%A.frontmatter %%A
      )
)

For those who want to see Hugo version in the window title.

run-server.cmd

@for /f "tokens=*" %%A in ('hugo version') do (title %%A)
hugo server
pause

This file must be run from the site folder.

1 Like

I put my sendto folder on the github https://github.com/mikhail73/sendto

1 Like