mandy
mandy

Reputation: 3

CMD start .exe with just one of multiple parameters

I would like my batch script to randomly choose one parameter on its own (from around 70 parameters eg. param1 - param70), without my input.

In addition to the random param, the exe has more parameters which always stay the same.

I dont know how to put this in code.

Here's an example of my thought:

param1=--abc
param2=--mno
param3=--xyz

./example.exe --hello --world --(param1 OR param2 OR param3)

which equals to:

./example.exe --hello --world --abc

or

./example.exe --hello --world --mno

or

./example.exe --hello --world --xyz

Upvotes: 0

Views: 355

Answers (3)

user6811411
user6811411

Reputation:

Handling 70 parameters Gerhards way will get tedious. I'd build a parameter array and get a random one.

:: Q:\Test\2018\04\27\SO_50059458.cmd
@Echo off&SetLocal EnableExtensions EnableDelayedExpansion

Rem Build param[] array and count params
Set Cnt=-1&Set "param= abc bcd cde def efg fgh ghi hij ijk jkl klm lmn mno"
Set "param=%param: ="&Set /a Cnt+=1&Set "param[!Cnt!]=%"
:: show array
Set param
:: get random # in Cnt
Set /a Rnd=%Random% %% Cnt 
echo Random %Rnd% out of %Cnt%
Echo .\example.exe --hello --!param[%Rnd%]!

Sample output:

> Q:\Test\2018\04\27\SO_50059458.cmd
param[0]=abc
param[10]=klm
param[11]=lmn
param[12]=mno
param[1]=bcd
param[2]=cde
param[3]=def
param[4]=efg
param[5]=fgh
param[6]=ghi
param[7]=hij
param[8]=ijk
param[9]=jkl
Random 10 out of 12
.\example.exe --hello --klm

Upvotes: 1

user7818749
user7818749

Reputation:

This can work in batch.You need to set each param though.

set /a numb=%random% %% 3
goto :param%numb%

:param0
Set "var=abc"
Goto :execute

:param1
Set "var=mno"
Goto :execute

:param2
Set "var=xyz"
Goto :execute

:execute
.\example.exe --hello --%var%

For 70 params you need to change %% 3 to %% 70

Upvotes: 4

henrycarteruk
henrycarteruk

Reputation: 13227

In powershell:

$params = "abc","mno","xyz"

& example.exe --hello --world --$(Get-Random -InputObject $params -Count 1)

Upvotes: 1

Related Questions