如何从文本文件中的特定行号设置变量? [英] 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 file. Then set the contents of that line number to a variable. Any ideas? Thanks!
推荐答案
类似的方法应该起作用
@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!
这篇关于如何从文本文件中的特定行号设置变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文