Urlencode-path script

Michael Tkhorev
Michael Tkhorev Script

I often need to insert links to local files/folders into Markdown documents. They should be clickable. To do this, I need to replace spaces in them with "%20", and add "file:///" at the beginning.


From a link like this:
E:\My Documents\File.txt

Make this:
file:///E:\My%20Documents\File.txt

How to automate this process:

Create a file:
urlencode-path.bat

With content:
@echo off
chcp 65001 > nul  & rem Fix text encoding
setlocal enabledelayedexpansion

:: Get input (drag & drop or manual)
set "input=%~1"
if "%input%"=="" (
    set /p "input=Enter path or text: "
)

:: Remove quotes if dragged & dropped
set "input=!input:"=!"

:: Replace spaces with %20
set "output=!input: =%%20!"

:: Check if it's a path (has :\ or /)
echo !output! | findstr /i "[\\/]" > nul
if !errorlevel! equ 0 (
    set "output=file:///!output!"
)

:: Display and copy to clipboard
echo Result: !output!
echo !output! | clip
echo Text copied to clipboard!
pause

Now you can Drag & Drop a File/Folder directly onto the bat file. A console window will open with the inscription:

Text copied to clipboard
Press any key to continue . . .