How to set a variable from a specific line number in a text file?
Ok so here is the situation. I want a batch file that will generate a random number which will be the line number it should read, read that line number from a text f开发者_StackOverflow中文版ile. Then set the contents of that line number to a variable. Any ideas? Thanks!
Something like this should work
@echo off
setlocal DisableDelayedExpansion
set MaxLine=10
set /a lineNr=%random% %% MaxLine
if %lineNr% EQU 0 (
set "strSkip="
) ELSE (
set "strSkip=skip=%lineNr%"
)
set /a lineNr+=1
for /F "usebackq %strSkip% delims=" %%a in ("text.txt") do (
set "line=%%a"
goto :break
)
:break
setlocal EnableDelayedExpansion
echo Line[%lineNr%]=!line!
精彩评论