怎样用批处理随机提取txt文档里的一行文字并显示出来

rt

这个是 test.txt 的内容:

1 aa
2 bb
3 cc
4 dd
5 ee
6 ff
7 gg
8 hh
9 ii

这个是 a.bat 的内容:

@echo off
setlocal enabledelayedexpansion

set file=test.txt
set line_num=0

for /f %%i in (%file%) do set /a line_num+=1

set /a "line=%random% %% %line_num%"
set /a line+=1

set line_num=0
for /f "tokens=*" %%i in (%file%) do (
    set /a line_num+=1
    if !line_num!==%line% (
        echo %%i
        goto :EOF
    )
)

这是测试输出:

C:\Documents and Settings\test\桌面>a.bat
7 gg

C:\Documents and Settings\test\桌面>a.bat
7 gg

C:\Documents and Settings\test\桌面>a.bat
8 hh

C:\Documents and Settings\test\桌面>a.bat
3 cc

C:\Documents and Settings\test\桌面>a.bat
5 ee

C:\Documents and Settings\test\桌面>a.bat
4 dd

C:\Documents and Settings\test\桌面>a.bat
3 cc


可以更改 a.bat 里的 test.txt 为你的文件即可。

温馨提示:答案为网友推荐,仅供参考
相似回答