文字尤斯一批随机行 [英] Random line of text ussing batch

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

问题描述

我希望有人能告诉我怎样可以从一个txt文件选择文本随机线,并将其设置在一个变量,我使用?

I was hoping someone could tell me how i could select a random line of text from a txt file and set it in a variable for me 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


  • 如果该文件是空的, 0 和随后的前$ P $使用它pssion将零错误碰上一个部门。因此,你还需要(进一步运行和prevent脚本如果计数确实是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%。这很可能是够你,但如果不是这样,您可以将范围扩展到2 30 -1使用的两个%RANDOM% 调用是这样的:

  • 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%
    

    另外一个需要注意的是,如果你愿意,你可以添加解释脚本的premature终止原因的消息。基本上,无论你想添加的消息,你只需要更换一个

    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天全站免登陆