开发者

How to set a variable from a specific line number in a text file?

开发者 https://www.devze.com 2023-02-15 23:46 出处:网络
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 s

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

精彩评论

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