简单批处理脚本随机数生成器

时间:2017-02-18 14:44:43

标签: batch-file random

我是新手在批处理脚本中使用%random%命令,我正在尝试创建一个批处理脚本来随机创建一个介于0和3之间的数字。我写了以下但每个实例(每次我双击BAT文件)我得到相同的号码' 1'。这违背了目的。这是基于所选视频文件的这个数字副本,因此它是随机的。

echo off 
set numb=" "
set /a numb=%random% / 10000 
echo %numb% :only used for testing 
pause :only used for testing

if %numb% LEQ 1 goto trailer
if %numb% EQU 2 goto dragon
if %numb% EQU 3 goto live

:dragon
echo Dragon
copy /Y "F:\Skyrim SSE\mod backup - files\Dragon flyby - BGS_Logo.bik" "D:\SteamLibrary\steamapps\common\Skyrim Special Edition\Data\Video\BGS_Logo.bik"
exit

:trailer
echo Trailer
copy /Y "F:\Skyrim SSE\mod backup - files\Official Trailer-BGS_Logo.bik" "D:\SteamLibrary\steamapps\common\Skyrim Special Edition\Data\Video\BGS_Logo.bik" 
exit

:live
Echo Live
copy /Y "F:\Skyrim SSE\mod backup - files\Dragonborne-live-BGS_Logo.bik" "D:\SteamLibrary\steamapps\common\Skyrim Special Edition\Data\Video\BGS_Logo.bik" 
exit

我希望尽可能简单,因为这并不需要复杂。请帮忙

1 个答案:

答案 0 :(得分:0)

以下是一种实现方法的示例:

@Echo Off

Set "_s=F:\Skyrim SSE\mod backup - files"
Set "_d=D:\SteamLibrary\steamapps\common\Skyrim Special Edition\Data\Video\BGS_Logo.bik"

Set/A "_r=%RANDOM%%%3"
>Nul Choice /C 012 /N /T 0 /D %_r%
If ErrorLevel 3 GoTo Dragon
If ErrorLevel 2 GoTo Trailer
If ErrorLevel 1 GoTo Live
GoTo :EOF

:Dragon
Echo(Dragon
Copy /Y "%_s%\Dragon flyby - BGS_Logo.bik" "%_d%"
GoTo EndIt

:Trailer
Echo(Trailer
Copy /Y "%_s%\Official Trailer-BGS_Logo.bik" "%_d%"
GoTo EndIt

:Live
Echo(Live
Copy /Y "%_s%\Dragonborne-live-BGS_Logo.bik" "%_d%"

:EndIt
GoTo :EOF

我使用CHOICE没有任何正当理由,我只是喜欢使用比较运算符IF 的一系列EQU, NEQ, LSS, LEQ, GTR, GEQ