The Bat Script Journey — Creating a Simple Batch Script to Pick a Random Name

Handhika Yanuar Pratama
3 min readFeb 9, 2024
Photo by Madara on Unsplash

Introduction

Batch scripting in Windows Command Prompt can be a powerful and efficient way to automate tasks and streamline processes. In this article, I am generating a simple batch script that reads a list of names from a text file and randomly selects one, declaring it as the winner.

This script is handy for scenarios like choosing a winner from a list of participants. So, from the previous article I have a list of names that contains 1000 employees.

I just want to select one name from the lists using the batch scripts.

Script

@echo off
setlocal enabledelayedexpansion

set "file_path=list-names.txt"

:: Count the number of lines in the file
for /f %%a in ('type "%file_path%" ^| find /c /v ""') do set "line_count=%%a"

:: Generate a random index and retrieve the corresponding name
set /a "random_index=!RANDOM! %% line_count"
set "count=0"

:: Read the file and set the winner
for /f "usebackq skip=%random_index% tokens=* delims=" %%a in ("%file_path%") do (
if !count! equ 0 (
set "random_name=%%a"
echo The Winner is: !random_name!
)
set /a "count+=1"
)

pause
endlocal

Output

Explanation

@echo off
setlocal enabledelayedexpansion

This section sets up the script by turning off command echoing (@echo off) and enabling delayed variable expansion (setlocal enabledelayedexpansion).

set "file_path=list-names.txt"

:: Count the number of lines in the file
for /f %%a in ('type "%file_path%" ^| find /c /v ""') do set "line_count=%%a"

The file_path variable is set to the path of the text file containing the list of names. The script then counts the number of lines in the file using the type and find commands, storing the result in the variable line_count.

:: Generate a random index and retrieve the corresponding name
set /a "random_index=!RANDOM! %% line_count"
set "count=0"

Here, a random index is generated based on the number of lines in the file. The script initializes a variable count to 0, which will be used to track the current line being processed.

:: Read the file and set the winner
for /f "usebackq skip=%random_index% tokens=* delims=" %%a in ("%file_path%") do (
if !count! equ 0 (
set "random_name=%%a"
echo The Winner is: !random_name!
)
set /a "count+=1"
)

This part of the script uses a for /f loop to read the lines from the file. The skip option is used to skip the first random_index lines. Once the loop reaches the line specified by random_index, it sets the random_name variable to the content of that line and echoes the winner to the console.

pause
endlocal

The script includes a pause command to keep the Command Prompt window open, allowing the user to view the winner before closing the window.

Finally, the endlocal command is used to end the local scope and ensure that any variables defined within the script do not persist outside of it.

Conclusion

In summary, this script reads a list of names from a text file, randomly selects one as the winner, and displays the result in the Command Prompt window. You can customize this script by modifying the file_path variable to point to your own text file with a list of names. Feel free to adapt and expand upon this script to suit your specific needs.

--

--