使用批处理的随机文本行 [英] Random line of text using batch

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

问题描述

如何从文本文件中随机选择一行文本并将其设置为变量以供使用?

How could one select a random line of text from a text file and set it in a variable to use?

推荐答案

这是另一种方法.它从命令行读取文件名,并使用FOR /L循环获取计算出的行号:

Here's yet another approach. It reads the file name from the command line and uses a FOR /L loop to get to the calculated line number:

@ECHO OFF
FOR /F "" %%I IN ('FIND /C /V "" ^<%1') DO SET /A lines=%%I
SET /A skip=%RANDOM%%%lines
<%1 (
  FOR /L %%I IN (1,1,%skip%) DO (
    SET /P line=
  )
  SET line=
  SET /P line=
)
ECHO(%line%

FOR /F循环只是获取文件中的行数(该方法是从 @Aacini的答案中借用的 ).

The FOR /F loop simply gets the number of lines in the file (the method is borrowed from @Aacini's answer).

然后用一个相当简单的公式计算文件中要跳过的行数.

A rather simplistic formula then calculates the number of lines to skip in the file.

接下来,读取文件. FOR /L循环仅使用SET /P指令消耗指定的行数.循环之后,又有一个SET /P命令读取最终回显的行.

Next, the file is read. The FOR /L loop merely consumes the specified number of lines using a SET /P instruction. Following the loop, one more SET /P command reads the line that is eventually ECHOed.

以上实现只是为了展示基本思想.并非没有问题,但其中一些问题很容易解决:

The above implementation is just to show the basic idea. It is not without issues, but some of them could easily be resolved:

  1. 没有测试该参数是否确实提供.如果不存在,脚本将中断.您可以在脚本的开头添加必要的检查,如下所示:

  1. There's no testing whether the parameter is indeed supplied. If it is absent, the script will break. You could add the necessary check at the beginning of the script like this:

IF "%~1"=="" GOTO :EOF

如果没有参数,此命令通过将控件发送到脚本末尾(GOTO :EOF)来终止脚本.

If there's no parameter, this command terminates the script by sending the control to the end of the script (GOTO :EOF).

指定的文件可能不存在.再一次,您可以在验证参数是否已提供之后就在开始时进行测试,以在必要时终止脚本:

The file specified might not exist. Again, you could test that at the beginning, just after verifying that the parameter is supplied, to terminate the script if necessary:

IF NOT EXIST %1 GOTO :EOF

  • 如果文件为空,则lines将为0,并且使用该文件的后续表达式将被除以零错误.因此,您还需要测试结果行数(如果计数确实为0,则防止脚本进一步运行).您可以通过在FOR /F循环之后添加以下行来做到这一点:

  • If the file is empty, the lines will be 0 and the subsequent expression using it will run into a division by zero error. Therefore, you'll also need to test the resulting line count (and prevent the script from running further if the count is indeed 0). You can do that by adding the following line just after the FOR /F loop:

    IF %lines%==0 GOTO :EOF
    

  • 就像我说的那样,该公式有些简单化.它不会产生大于32767的数字,这是%RANDOM%的限制.对于您来说这可能就足够了,但是如果不是这样,您可以使用两个 %RANDOM%这样的调用将范围扩展到2 30 -1:

  • Like I said, the formula is somewhat simplistic. It doesn't produce a number greater than 32767, which is the limitation of %RANDOM%. That might well be enough for you, but in case it is not, you could extend the range to 230-1 using two %RANDOM% calls like this:

    SET /A skip=(%RANDOM%*32768+%RANDOM%)%%lines
    

  • 因此,这里再次是相同的脚本,经过修改以解决上述问题:

    So, here's the same script again, amended to address the above mentioned issues:

    @ECHO OFF
    IF "%~1"=="" GOTO :EOF
    IF NOT EXIST %1 GOTO :EOF
    FOR /F "" %%I IN ('FIND /C /V "" ^<%1') DO SET /A lines=%%I
    IF %lines%==0 GOTO :EOF
    SET /A skip=(%RANDOM%*32768+%RANDOM%)%%lines
    <%1 (
      FOR /L %%I IN (1,1,%skip%) DO (
        SET /P line=
      )
      SET line=
      SET /P line=
    )
    ECHO(%line%
    

    另一个注意事项是,如果您愿意,可以添加说明脚本过早终止的原因的消息.基本上,无论您要将消息添加到何处,都只需替换单个

    One other note is that, if you like, you can add messages explaining the reason for the premature termination of the script. Basically, wherever you want to add the message, you'll just need to replace the single

    GOTO :EOF
    

    使用

    (ECHO your message & GOTO :EOF)

    例如:

    IF NOT EXIST %1 (ECHO Error! File not found & GOTO :EOF)
    

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

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