There are two problems in your code.
The first has been pointed by Stephan. If you read the answers to this question, you will see that the parser replaces variables referenced with percents before variables referenced with exclamation marks. So when the parsers tries to handle %Line_!Counter!%
, !Counter!
is still not replaced in the line so %Line_!Counter!%
is an undefined variable and is replaced with nothing. The inverse (!Line_%Counter%!
) works because when the parser reaches the line, the first substitution is the percent variable and the last the exclamation mark variable.
The second is a logic error. The line
set /a Counter=%random% * 100 / 32768 + 1
will not work as intended if the file has more or less than 100 lines. If it has more, the higher numbered lines will never be selected. If it has less lines, a high numbered non existing line can be selected and as it does no exist, you will again get an echo is off
message trying to echo the variable.
@echo off
setlocal enabledelayedexpansion
set Counter=0
for /f "tokens=* delims=" %%x in (Lists.txt) do (
set /a Counter+=1
set "Line_!Counter!=%%x"
)
set /a "selected=%random% %% Counter + 1"
echo %selected%
echo "!Line_%selected%!"
pause