Skip to main content

How to Create a Mad Libs Generator in Batch Script

One of the most engaging ways to learn user interaction in batch scripting is by building a Mad Libs generator. This classic word game relies on collecting user input and inserting it into a predefined story, making it a perfect introduction to handling dynamic text in scripts.

This guide will show you how to prompt users for different types of words, store their responses in variables, and combine those values into a formatted output. Along the way, you’ll practice input handling, variable usage, and string construction, core skills for creating interactive command-line programs.

The Concept of Variable Injection

The core mechanism behind a Mad Libs generator is variable injection (or string interpolation). In Batch Script, you define a variable using the set command and later inject it into a string of text using % delimiters.

For instance, if you define a noun:

set "NOUN=dog"
echo The %NOUN% jumped over the fence.

The Command Prompt evaluates %NOUN% at runtime and outputs: The dog jumped over the fence.

Building a full game requires scaling this concept up, asking the user to provide their own nouns, verbs, and adjectives dynamically using set /p before the story is built.

Gathering User Input with set /p

To build the story, we first need to collect the raw data. The set /p command halts the batch execution and waits for the user to type something and press Enter. The string they typed is then assigned to the specified variable.

@echo off
setlocal enabledelayedexpansion
title Mad Libs Generator

echo Let's play Mad Libs! I need you to give me some words.
echo ------------------------------------------------------

set /p "ADJECTIVE1=Enter an adjective: "
set /p "NOUN1=Enter a noun: "
set /p "PLURAL_NOUN=Enter a plural noun: "
set /p "VERB_PAST=Enter a verb (past tense): "
set /p "ADJECTIVE2=Enter another adjective: "

By adding double quotes around the VARIABLE=Prompt Text assignment, we ensure trailing spaces in our prompt look correct and protect against accidental special character evaluation.

Formatting the Final Output

Once all variables are loaded into memory, creating the story is as simple as layering echo statements with your variables wedged into the sentences.

echo.
echo ======================================================
echo THE GENERATED STORY
echo ======================================================
echo.
echo One day, a !ADJECTIVE1! !NOUN1! decided to go on
echo an epic adventure.
echo.
echo It packed a bag full of !PLURAL_NOUN! and !VERB_PAST!
echo right out the front door.
echo.
echo To everyone's surprise, the journey turned out
echo to be incredibly !ADJECTIVE2!.
echo.
echo ======================================================
pause
exit /b 0

When run, if the user typed sparkly, dragon, sandwiches, flew, and hilarious, the script produces a customized, coherent block of text.

Common Wrong Cases and Best Practices

A common pitfall for beginning batch developers is failing to account for "empty" variables. Unlike robust programming languages that enforce typing rules, if a user simply presses Enter without typing a word, Batch will leave the variable undefined or replace it with whatever was previously stored in memory.

The Wrong Way: Assuming Valid Input

When developers write set /p without clearing the variable beforehand or checking if the user actually typed something, the story can break or print literal %word% placeholders.

Wrong Code Example:

@echo off
set /p "ADJECTIVE=Enter an adjective: "

:: If the user immediately presses enter, the variable remains undefined!
echo The %ADJECTIVE% dog barked.

What Happens: If the variable is undefined, the output might literally read The %ADJECTIVE% dog barked. instead of inserting a blank space or a fallback word, completely ruining the immersion of the script. Or worse, if %ADJECTIVE% was defined in a previous run of the script on the same command prompt instance, the old word will bleed into the new story!

The Correct Way: Clearing Variables and Input Validation

To write a professional level prompt loop, always clear the variable before asking for it. If you want to ensure the user does not skip a prompt, use a goto loop to force answers.

Correct Code Example:

@echo off
setlocal enabledelayedexpansion

:GET_ADJECTIVE
set "ADJECTIVE="
set /p "ADJECTIVE=Enter an adjective: "

:: Validating if the string is empty
if not defined ADJECTIVE (
echo You cannot leave this blank! Please type an adjective.
goto GET_ADJECTIVE
)

echo The !ADJECTIVE! dog barked loudly!

What Happens: The set "ADJECTIVE=" line completely zeroes out the variable in memory. If the user hits enter on an empty string, the if not defined ADJECTIVE check catches it without expanding the variable at all, preventing a script break from special characters and looping back to prompt them again until they provide a word.

Full Script Implementation

Putting the entire process together utilizing best practices, we can build a robust, safe, and entertaining script. Start your text editor, copy the following code, and save it as madlibs.bat.

@echo off
setlocal enabledelayedexpansion
title Ultimate Batch Mad Libs
color 0A

echo ===========================================
echo THE ULTIMATE MAD LIBS GENERATOR
echo ===========================================
echo Please provide the following words to build
echo a hilarious and custom story.
echo ===========================================
echo.

:: Loop to strictly get NOUN1
:GET_NOUN1
set "NOUN1="
set /p "NOUN1=1. Enter a noun (e.g., cat, car, pizza): "
if not defined NOUN1 goto GET_NOUN1

:: Loop to strictly get ADJECTIVE1
:GET_ADJ1
set "ADJECTIVE1="
set /p "ADJECTIVE1=2. Enter an adjective (e.g., smelly, shiny): "
if not defined ADJECTIVE1 goto GET_ADJ1

:: Loop to strictly get VERB1
:GET_VERB1
set "VERB1="
set /p "VERB1=3. Enter a verb ending in 'ing' (e.g., running): "
if not defined VERB1 goto GET_VERB1

:: Loop to strictly get PLACE1
:GET_PLACE1
set "PLACE1="
set /p "PLACE1=4. Enter a place (e.g., the moon, Walmart): "
if not defined PLACE1 goto GET_PLACE1

:: Loop to strictly get PLURAL_NOUN
:GET_PLURAL
set "PLURAL_NOUN="
set /p "PLURAL_NOUN=5. Enter a plural noun: "
if not defined PLURAL_NOUN goto GET_PLURAL

:: Clear the screen for dramatic effect
cls
echo Generating your completely unique story...
timeout /t 2 >nul
echo.

echo =======================================================
echo THE GREAT ADVENTURE
echo =======================================================
echo There once was a !ADJECTIVE1! !NOUN1! that spent every
echo single day !VERB1! near !PLACE1!.
echo.
echo Everyone in town thought the !NOUN1! was completely
echo crazy, but it didn't care. It was busy collecting
echo massive amounts of !PLURAL_NOUN! to build a spaceship.
echo.
echo And you know what? The !ADJECTIVE1! !NOUN1! succeeded.
echo It flew away, leaving a trail of !PLURAL_NOUN! behind.
echo =======================================================
echo.

pause
info

Notice the use of setlocal enabledelayedexpansion and the ! exclamation marks for variable wrapping instead of %. This is a professional habit that guarantees variables containing accidental special characters (like & or |) do not break the script logic during the if evaluations or echo output.

Conclusion

Creating a Mad Libs generator in Batch Script teaches more than just a silly game; it drills the fundamental syntax of data gathering, state clearing, input validation, and secure variable expansion. By treating user input as untrusted and forcing empty checking loops, you ensure your execution environments remain stable and predictable.

Take this foundational code and expand upon it. Try building longer stories, randomizing the questions asked, or even saving the generated outputs to a permanent text file to read later!