Windows批处理 - 从文本文件中随机线 [英] Windows Batch - Random Line from Text File

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

问题描述

我不能让下面的东西的工作。我有400行和生长的文本文件。我需要一个批处理脚本挑选一个随机线形成文件。

I can't get the following thing working. I have a text file with 400 lines and growing. I need a batch script that picks a single random line form that file.

我试图修改一些code,做了随机化,但有时它选择一个数字,大于400和脚本返回一个错误:

I tried to modify some code that does the randomization but sometimes it picks a number that is larger than 400 and the script returns an error:

set "file=C:\Users\User\Desktop\resolutions.txt"
for /f %%N in ('findstr "." "%file%"^|find /c /v ""') do set lines=%%N
set /a "iterations=%random% %% 1 + 1"

set /a randomfixed = %random% * %Lines% / 32767 + 1

for /f "skip=%randomfixed% tokens=* delims=" %%j IN (C:\Users\User\Desktop\useragents.txt) do (
set /a N+=1
    set var!N!=%%j
)
@echo %var1%

我想有来处理这更简单的方法?任何帮助AP preciated!

I guess there is an easier way to handle this? Any help is appreciated!

推荐答案

试试这个:

@echo off &setlocal
set /a count=0
for /f "tokens=1delims=:" %%i in ('findstr /n "^" "file.txt"') do set /a count=%%i
set /a rd=%random%%%count
if %rd% equ 0 (set "skip=") else set "skip=skip=%rd%"
set "found="
for /f "%skip%tokens=1*delims=:" %%i in ('findstr /n "^" "file.txt"') do if not defined found set "found=%%i"&set "var=%%j"
echo.random line %found%: %var%
endlocal

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

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