Use case
You need a large text file quickly for testing integrations, stress-testing parsers, load tests, or demoing file-handling logic. The simplest approach on Windows is to start with a single line and repeatedly concatenate the file with itself — this doubles the line count each pass and reaches large sizes fast (exponential growth).
Explanation:
This batch script creates an output file with a single sample line and then doubles its size repeatedly by concatenating the file into a temporary file and back. Because every iteration multiplies the line count by two, you quickly reach tens or hundreds of thousands of lines with only a small number of iterations. The example below runs 17 doubling passes, producing 2^17 = 131,072
lines.
Code (copy-paste into a .bat
file or .cmd file)
@echo off
setlocal EnableDelayedExpansion
:: Line content
set "line=This is the sample line"
:: Output file (adjust path if needed)
set "outfile=%USERPROFILE%\Desktop\output.txt"
if exist "%outfile%" del "%outfile%"
:: Start with 1 line
echo %line%>"%outfile%"
:: Double the file until it reaches ~100,000+ lines
for %%i in (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17) do (
type "%outfile%" >> "%outfile%.tmp"
type "%outfile%.tmp" >> "%outfile%"
del "%outfile%.tmp"
)
echo Done!
pause
Step-by-step solution / how it works
-
Set up environment
@echo off
hides command echoing.setlocal EnableDelayedExpansion
enables advanced variable handling (safe practice for scripts that modify variables in loops).
-
Define the sample line
set "line=This is the sample line"
— change the text inside quotes to whatever content you want repeated.
-
Define and clear output file
set "outfile=%USERPROFILE%\Desktop\output.txt"
places file on Desktop; change path as required.if exist "%outfile%" del "%outfile%"
removes any previous file with the same name.
-
Seed file
echo %line%>"%outfile%"
creates the file with a single line to start.
-
Double the file repeatedly
- The
for %%i in (...) do (...)
loop runs 17 times (you can change the number to control final size). - Inside loop:
type "%outfile%" >> "%outfile%.tmp"
writes the current file content into a temp file (one copy).type "%outfile%.tmp" >> "%outfile%"
appends that temp file back to the original — now the original contains the old content plus the appended copy → doubled size.del "%outfile%.tmp"
removes the temp file.
- The
-
Finish
echo Done!
informs completion andpause
keeps the console open to view the message (press any key to exit).
How to choose number of iterations
- Start with 1 line. Each iteration doubles the line count: after
n
iterations you have2^n
lines.- 10 iterations → 1,024 lines
- 16 iterations → 65,536 lines
- 17 iterations → 131,072 lines (the sample script)
- If you want a specific line count, choose
n = ceil(log2(desired_lines))
.
Tips & cautions
- Disk space & memory: big files can consume significant disk space and may be slow on slow disks. Use caution on low-storage systems.
- Encoding:
echo
/type
produce ANSI encoding by default. If you need UTF-8, consider using PowerShell (example below) or ensure callers handle ANSI. - Permissions: run in a location where you have write access (Desktop is safe for user-run scripts).
- Performance: doubling is fast but each
type
reads and writes the whole file — for extremely large sizes, consider streaming approaches or generating lines programmatically. - Cleanup: remember to delete the generated file after tests.
No comments:
Post a Comment