.txt文件的批处理随机行 [英] Batch random line for a .txt file

查看:176
本文介绍了.txt文件的批处理随机行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个批处理文件,我想在主屏幕上显示一个初始画面.我希望批处理文件从文本文件中选择一条随机行并将其回显.文件名将为splashes.txt. 就像这样:

I'm writing a batch file and I wanted to display a splash in the main screen. I wanted the batch file to pick a random line from a text file and ECHO it. The file name would be splashes.txt. And in it would be like:

More addicting the lemons   
Apples.  
This is a splash  
Test

该批处理文件将根据其行选择一个随机引用. 就像补丁文件选择了第2行一样,它会回显"Apples".

The batch file would pick a random quote based on its line. Like if the patch file picked line 2 it would ECHO "Apples."

请注意,我正在使用Windows 8. 有什么想法吗?

Note that I'm using Windows 8. Any ideas?

推荐答案

虽然我与其他人一样,您应该自己尝试,但这是一个相对简单的脚本,应作为学习的一个很好的工作示例:

While I agree with others that you should attempt this on your own, this is a relatively straight-forward script which should serve as a good working example for learning:

@ECHO OFF
SETLOCAL EnableDelayedExpansion EnableExtensions

REM Source file.
REM The first line on this file should be blank as it will never be selected.
REM Additionally, this file should have no empty lines on the end.
SET TextFile=text.txt

REM Determine the number of lines.
SET NumLines=0
FOR /F "usebackq tokens=* delims=" %%A IN (`TYPE %TextFile%`) DO SET /A NumLines=!NumLines!+1

REM Pick a random line.
SET /A RandomLine=(%RANDOM% %% %NumLines) + 1

REM Prevent skipping all the lines.
IF "%RandomLine%"=="%NumLines%" SET RandomLine=1

REM Print the random line.
FOR /F "usebackq tokens=* skip=%RandomLine% delims=" %%A IN (`TYPE %TextFile%`) DO (
    ECHO %%A
    REM We are done. Stop the script.
    GOTO Finish
)

:Finish
ENDLOCAL


太近了-但不太完全. SKIP始终至少为1(因为SKIP=0无效),因此永远都不能选择文件的第一行.


So close - but not quite. SKIP will always be at least 1 (since SKIP=0 is invalid) hence the first line in the file can never be selected.

这是我从上面获得的文件,带有一些挠痒痒的感觉.由于我的工作方式,我还更改了文件名.我正在使用q27829742.txt包含已发布的行.

This is a file I derived from the above with a few tickles. I've also changed the filename because of the way I work. I'm using q27829742.txt containing the lines posted.

@ECHO OFF
SETLOCAL
SETLOCAL EnableDelayedExpansion EnableExtensions

REM Source file.
REM The first line on this file should be blank as it will never be selected.
REM Additionally, this file should have no empty lines on the end.
SET "TextFile=q27829742.txt"

REM Determine the number of lines.
FOR /f %%a IN ('type "%textfile%"^|find /c /v ""') DO SET /a numlines=%%a

REM Pick a random line.
SET /A RandomLine=(%RANDOM% %% %NumLines%)

REM Prevent skipping all the lines.
IF "%RandomLine%"=="0" (SET "RandomLine=") ELSE (SET "RandomLine=skip=%randomline%")

REM Print the random line.
FOR /F "usebackq tokens=* %RandomLine% delims=" %%A IN (`TYPE %TextFile%`) DO (
    ECHO %%A
    REM We are done. Stop the script.
    GOTO Finish
)

:Finish
ENDLOCAL

find /v /c方法对文件中的行进行计数(查找不匹配(/v)"(因此表示所有行)的文件行,并对它们进行计数(/c)-效率更高.

The find /v /c method counts lines in the file (find files lines that don't match (/v) "" (so that means all lines) and count them (/c) - simply more efficient.

Pick-random-number:删除+ 1会产生结果0..(numlines-1),它是>的 actual 行数.

Pick-random-number : removing the + 1 produces a result 0..(numlines-1) which is the actual number of lines to skip.

问题在于skip=0是无效的,因此构造一个字符串,该字符串应为另一个 empty (对于0)或"skip = ..."(否则)-都可以包含在for /f命令选项中.

Problem there is that skip=0 is invalid, so construct a string which is eother empty (for 0) or "skip=..." (otherwise) - all ready to be included in the for /f command-options.

语法SET "var=value"(值可能为空)用于确保分配的值中不包括行尾的任何空格. set /a可以安全地无引号"使用.

The syntax SET "var=value" (where value may be empty) is used to ensure that any stray spaces at the end of a line are NOT included in the value assigned. set /a can safely be used "quoteless".

这篇关于.txt文件的批处理随机行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆