Skip to content

How to Build a Simple Key-Value Store (Dictionary) in Batch Script

Learn how to create a dictionary-like key-value store in Windows Batch scripting using variable namespacing to store, retrieve, and manage configurations like server IPs or user access levels.

Most advanced scripts require a way to map specific names to specific values, for example, mapping a "Server Name" to its "IP Address" or a "User ID" to their "Access Level". In programming, this is called a Dictionary or a Key-Value Store. While Batch doesn't have a native dictionary object, we can simulate one effectively using environment variable namespaces.

In this guide, we will demonstrate how to store, retrieve, and list data using a dictionary pattern.

The Strategy: Variable Namespacing

The secret to a Batch dictionary is using a unique prefix (a "Namespace") for your variables. Instead of set "Value=John", you use set "DB_User01=John". This allows you to manage your data as a single collection.

Implementation Script

@echo off
setlocal enabledelayedexpansion

:: 1. Store Values (SET)
:: We use a "KV_" prefix to create a separate namespace for our dictionary.
set "KV_AppVersion=2.4.1"
set "KV_AdminName=Alice"
set "KV_MainServer=192.168.1.50"

:: 2. Retrieve a Specific Value (GET)
set "target=MainServer"
:: To expand a dynamic variable (KV_!target!), we use a 'for' loop trick.
:: This is safer and faster than using the 'call echo' method.
for /f "delims=" %%V in ("KV_!target!") do (
    echo The !target! is: !%%V!
)

:: 3. Interactive Lookup
echo.
set /p "key=Enter settings name to look up: "

:: Reset lookup and then retrieve the value safely
set "lookup="
for /f "delims=" %%V in ("KV_!key!") do (
    set "lookup=!%%V!"
)

if defined lookup (
    echo [FOUND] !key! = !lookup!
) else (
    echo [ERROR] Key '!key!' does not exist.
)

:: 4. List All Key-Value Pairs
echo.
echo --- CURRENT CONFIGURATION ---
:: 'set KV_' lists all variables starting with the prefix.
for /f "tokens=1,* delims==" %%A in ('set KV_ 2^>nul') do (
    set "fullkey=%%A"
    :: Extract the key after the "KV_" prefix (first 3 characters)
    set "keyname=!fullkey:~3!"
    echo !keyname! : %%B
)

endlocal
pause

Persisting the Dictionary to Disk

If you want your dictionary to "Remember" values even after the script closes, you can save the set commands to a file and load them later.

:: Save to file
(for /f "tokens=1,* delims==" %%A in ('set KV_ 2^>nul') do (
    echo set "%%A=%%B"
)) > config.dat

:: Load from file later
for /f "usebackq delims=" %%i in ("config.dat") do %%i

Why Use a Key-Value Store?

  1. Configuration Management: Storing all your script's settings (paths, timeouts, server names) in one place so they can be easily modified without digging through the logic.
  2. Lookup Tables: Mapping short codes (e.g., NY, CA) to full strings (e.g., New York, California).
  3. Dynamic Tracking: Keeping track of which tasks have been completed (set "TASK_5=DONE") during a long-running automation process.

Important Limitations

  1. Special Characters: Variable names (keys) should avoid symbols like & or | to prevent the Batch interpreter from trying to execute them. Stick to alphanumeric characters and underscores.
  2. Memory Limits: Variables use your environment's memory. While you can store thousands of small key-value pairs, an extremely large dictionary may eventually run out of space.
  3. Global vs Local: If you use setlocal, your dictionary will vanish when the script ends. This is intentional for script isolation. Use the disk persistence method above if you need values to survive across sessions.

Conclusion

Building a key-value store is a foundational step in creating "Intelligent" scripts that can manage complex datasets. By using variable namespacing, you turn a simple command-line environment into a flexible, searchable database. This technique is essential for professional configuration management, allowing you to build tools that are easy to update, easy to audit, and capable of handling dynamic user data with ease.

Navigation