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

前端 未结 6 782
挽巷
挽巷 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 00:55

    My solution is essentially identical to Aacini's but with a few differing details:

    @echo off
    
    setlocal enabledelayedexpansion
    set i=0
    for /f "tokens=*" %%x in (%1) do (
        set line[!i!]=%%x
        set /a i += 1
    )
    set /a j=%random% %% %i%
    set ip=!line[%j%]!
    

    The file name is an argument to this batch.

提交回复
热议问题