开发者

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!
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜