bat,如何随机地显示一个文本文件的第N行

来源:百度知道 编辑:UC知道 时间:2024/06/08 13:05:08
如题所示,是想随机地产生一个数字N,然后显示出指定的文本文件的第N行

谢谢!

@echo off
setlocal
REM %random:~0,n%的n为产生数字的最多位数
Set N=%random:~0,1%
:getfilename
Set /p file=请拖放一个文件到这里:
If not defined file Goto getfilename
If not exist %file% (Echo 错误:文件%file%无法找到。& Exit /b 1)
If "%N%" == "1" For /f "usebackq tokens=1,*" %%i In (%file%) Do (Echo 第%N%行:%%i %%j&endlocal & Exit /b 0)
Set /a skip=N-1
For /f "usebackq tokens=1,* skip=%skip%" %%i In (%file%) Do (Echo 第%N%行:%%i %%j&endlocal & Exit /b 0)