Randomly select a line out of a text file and set that line as a variable

前端 未结 6 769
挽巷
挽巷 2021-01-22 00:29

How to randomly select a line/ip out of a text file, and set it as a variable.

I mean something like:

set IP=TheLine/IPFromOfTheFile
6条回答
  •  一整个雨季
    2021-01-22 01:11

    Try this:

    @echo off
    setlocal EnableDelayedExpansion
    set "file=somefile.txt"
    set "lines=0"
    for /F "usebackq delims=" %%a in ("%file%") do set /a "lines+=1"
    set /a "selected=%random%%%%lines%"
    set "lines=0"
    for /F "usebackq delims=" %%a in ("%file%") do (
    if !lines! equ !selected! set "line=%%a"
    set /a "lines+=1"
    )
    echo %line%
    pause
    

提交回复
热议问题