Skip to main content

How to Generate Lorem Ipsum Text in Batch Script

Lorem Ipsum is the standard placeholder text used in the design and development industries to demonstrate the visual form of a document or a typeface without relying on meaningful content. While there are countless online generators, having a local tool can be incredibly useful for automated testing, file system stress tests, or quick UI prototyping. In this guide, we will learn how to create a custom Lorem Ipsum generator directly in the Windows Command Prompt using Batch Script.

Generating text in Batch requires a creative approach since the language lacks a native "random text" function. We will solve this by building a dictionary of words and using loops to assemble them into sentences and paragraphs.

The Strategy: Building a Modular Generator

To create a realistic Lorem Ipsum generator, we shouldn't just print a fixed block of text. Instead, we want a script that can:

  1. Store a collection of classic Latin words.
  2. Randomly select words to form sentences.
  3. Combine sentences into paragraphs.
  4. Allow the user to specify the amount of text needed.

Prerequisites

You should have a basic understanding of FOR loops and variable manipulation in Batch. We will extensively use enabledelayedexpansion to handle the dynamic construction of our text strings.

Setting Up the Dictionary

The first step is to define our word bank. We will use a pseudo-array to store the standard Lorem Ipsum words.

set "w1=lorem"
set "w2=ipsum"
set "w3=dolor"
set "w4=sit"
set "w5=amet"
:: ... and so on ...

By using a large enough collection of words, the resulting text will feel varied and natural for a placeholder.

Creating the Core Logic

The primary mechanism involves choosing a random number and using it to grab a word from our dictionary.

Using the Random Factor

In Batch, %RANDOM% allows us to pick a word index. If we have 50 words in our dictionary, we use set /a "idx=(%RANDOM% %% 50) + 1".

Admonition: Performance

Batch Script is not optimized for heavy string concatenation. If you try to generate 10,000 words in a single string variable, the script will slow down significantly or hit the 8,191-character limit for environment variables.

Common Pitfalls: Wrong vs Correct

The Wrong Way: Infinite Concatenation

Many developers try to build the entire output in one variable before printing it.

:: WRONG WAY
set "output="
for /L %%i in (1,1,1000) do (
set /a "idx=(!RANDOM! %% 10) + 1"
for %%j in (!idx!) do set "output=!output! !word%%j!"
)
echo !output!

Output Concern: This approach fails at scale because the output variable will exceed the 8,191-character limit for environment variables, resulting in truncated text or a "The input line is too long" error.

The Correct Way: Streaming Output

Instead of storing everything, we use the <nul set /p trick to print each word directly to the console without a newline, bypassing variable length limits entirely. The full implementation below demonstrates this approach.

Implementing the Lorem Ipsum Script

Here is a robust implementation that allows you to specify how many words you want.

@echo off
setlocal enabledelayedexpansion

:: --- Configuration ---
set "dictionary_size=30"
set "total_words=%~1"

:: If no argument is provided, default to 50 words
if not defined total_words set "total_words=50"

:: --- Word Bank ---
set "w1=lorem" & set "w2=ipsum" & set "w3=dolor" & set "w4=sit" & set "w5=amet"
set "w6=consectetur" & set "w7=adipiscing" & set "w8=elit" & set "w9=sed" & set "w10=do"
set "w11=eiusmod" & set "w12=tempor" & set "w13=incididunt" & set "w14=ut" & set "w15=labore"
set "w16=et" & set "w17=dolore" & set "w18=magna" & set "w19=aliqua" & set "w20=ut"
set "w21=enim" & set "w22=ad" & set "w23=minim" & set "w24=veniam" & set "w25=quis"
set "w26=nostrud" & set "w27=exercitation" & set "w28=ullamco" & set "w29=laboris" & set "w30=nisi"

:: --- Generation ---
echo.
echo Generating %total_words% words of Lorem Ipsum...
echo.

for /L %%i in (1,1,%total_words%) do (
set /a "idx=(!RANDOM! %% %dictionary_size%) + 1"

REM Resolve idx into a for-variable for clean delayed expansion lookup
for %%j in (!idx!) do <nul set /p "=!w%%j! "

REM Add a newline every 10 words for readability
set /a "mod=%%i %% 10"
if !mod! equ 0 echo.
)

echo.
echo.
echo --- End of Generation ---
pause

Key Techniques Explained

  1. <nul set /p "=text ": This is the most reliable way in Batch to print text to the console without automatically adding a line break. This allows us to build sentences word by word on a single line.
  2. for %%j in (!idx!): Because Batch cannot nest delayed expansion directly inside another delayed expansion (writing !w!idx!! causes the ! delimiters to pair incorrectly), we transfer the value of !idx! into a for loop variable %%j. The for-variable %%j is expanded before delayed expansion, so !w%%j! correctly becomes !w5! and then resolves to the stored word.
  3. Modulo for Newlines: if !mod! equ 0 echo. checks if the current word count is a multiple of 10. If so, it prints a newline, preventing the text from scrolling horizontally as one endless line.
  4. Command Line Arguments: The script uses %~1 to accept the number of words from the command line (e.g., lorem.bat 100).

Advanced Usage: Saving to a File

If you need to generate a text file for testing purposes, you can redirect the output of the entire script or a specific block.

:::tip[Admonition: File Redirection To save the generated text to a file named sample.txt, run the script like this: lorem_generator.bat 200 > sample.txt :::

Best Practices for Text Generation

  • Punctuation: To make the text more realistic, consider adding a random chance to append a comma or a period to the end of a word.
  • Capitalization: Capitalize the first word of every N words to simulate the start of a sentence.
  • Variable Scope: Always use setlocal to ensure your word bank variables don't clutter the user's global environment after the script finishes.

Summary

Generating Lorem Ipsum in Batch Script is a fantastic exercise in handling string output and randomization. While Batch isn't the first choice for complex text processing, the techniques learned here, like non-newline printing, pseudo-arrays, and the for variable resolution trick, are transferable to many other automation tasks. Whether you need a quick block of text for a mockup or a large file for testing, this generator provides a lightweight, dependency-free solution.