Can BAT file pick this or that?

Scorpion Ghost

Jr. VIP
Jr. VIP
Joined
Mar 22, 2013
Messages
5,454
Reaction score
4,978
Website
smmorigins.com
So say you run a BAT file to open a program. This:

Code:
@ECHO OFF
Timeout /T 15
start C:\"Some folder"\"Other folder"\SomeProgram.exe

Can a BAT file be made to choose either this or that? For example:

Code:
@ECHO OFF
Timeout /T 15
start C:\"Some folder"\"Other folder"\SomeProgram.exe OR C:\"Some folder"\"Other folder"\SomeProgram1.exe

And then the BAT file decides at random whether to open the first one or the second one.

I did the google fuck, tried various ways of searching, but just didn't find anything relevant.

Possible?
 

reuse

Jr. VIP
Jr. VIP
Joined
Feb 9, 2013
Messages
319
Reaction score
498
Yes, you can use %RANDOM% and you can control the range.

So if you want to run half the time, pick from 0 or 1 after generating random number between 0 and 1.
 

bartosimpsonio

Jr. Executive VIP
Jr. VIP
Joined
Mar 21, 2013
Messages
20,386
Reaction score
24,233
Website
www.theglobeandmail.com

Scorpion Ghost

Jr. VIP
Jr. VIP
Joined
Mar 22, 2013
Messages
5,454
Reaction score
4,978
Website
smmorigins.com
Yeah thanks guys but i dont understand the stuff in the threads you shared. I cant connect the dots.

I dont need any user input or to decide which BAT file runs or any fancy stuff. All i need is:

BAT file runs -> open Program A OR Program B -> the end

My BAT file sits in my Startup folder and runs once per day, after my computer is restarted, once per day.

The file opens various programs, always the same. But id like to add an extra program except i want it to choose between 3 different programs at random each time, so that each day one of the 3 programs runs until the next restart.

The links you guys shared lead to insanity :p thanks though
 

HyperEVmedia

Junior Member
Joined
Jan 5, 2017
Messages
145
Reaction score
186
Yeah thanks guys but i dont understand the stuff in the threads you shared. I cant connect the dots.

I dont need any user input or to decide which BAT file runs or any fancy stuff. All i need is:

BAT file runs -> open Program A OR Program B -> the end

My BAT file sits in my Startup folder and runs once per day, after my computer is restarted, once per day.

The file opens various programs, always the same. But id like to add an extra program except i want it to choose between 3 different programs at random each time, so that each day one of the 3 programs runs until the next restart.

The links you guys shared lead to insanity :p thanks though
It's very easy.
You generate a random number:
set /a num=%random% %%100 ; this generates a number between 0-99

Then you make an if statement:
IF %num%<50 (shit you want to do A)
ELSE (shit you want to do b)

The real key is %WHYISANYONEPROGRAMMINGBATCHIN2021INSTEADOFPYTHONORANYOTHERREALLANGUAGE%
Because batch is a surefire way to solve things without the need to install anything on a Windows system, and sometimes that really spares a lot of time.
 

Scorpion Ghost

Jr. VIP
Jr. VIP
Joined
Mar 22, 2013
Messages
5,454
Reaction score
4,978
Website
smmorigins.com
It's very easy.
You generate a random number:
set /a num=%random% %%100 ; this generates a number between 0-99

Then you make an if statement:
IF %num%<50 (shit you want to do A)
ELSE (shit you want to do b)


Because batch is a surefire way to solve things without the need to install anything on a Windows system, and sometimes that really spares a lot of time.

Yeah exactly what I wanted to say about batch :p

---

Before I continue, I don't really know much about batch files and the code they use. I can do a little coding in iMacro and even Greasemonkey, but I don't really use batch files.

Once upon a time I needed a way to auto-start many programs when the PC turns on. I went to google, I ended up in windows task scheduler. It was a complicated and slow piece of crap. So then I looked more, and found some freeware program. That worked, but still had issues and was slow, and anyway obviously I had some issue with it because I kept looking. Then I found bat files, and it seemed simple and easy. I figured out how to create a bat file, put it in the Startup folder, and poof it worked and worked well and perfect and continues to work all day every day.

But that's all I know, I don't really understand this code at all, total noob...

So now that I got that out of the way, I can't get this to work. I tried a couple of things:

Code:
@ECHO OFF
set /a num=%random% %%100 ;
IF %num%<50 (start C:\"Programs"\"Program"\Program1.exe)
ELSE (start C:\"Programs"\"Program"\Program1.exe)

Code:
@ECHO OFF
set /a num=%random% %%100
IF %num%<50 (start C:\"Programs"\"Program"\Program1.exe)
ELSE (start C:\"Programs"\"Program"\Program1.exe)

Code:
@ECHO OFF
set /a num=%random% %%100 ;
IF %num%<50 start C:\"Programs"\"Program"\Program1.exe
ELSE start C:\"Programs"\"Program"\Program1.exe

None of these worked. I'm probably making a very noob and obvious mistake, but I don't know this code at all.

This works haha:

Code:
@ECHO OFF
start C:\"Programs"\"Program"\Program1.exe

What's the problem?
 
Last edited:

reuse

Jr. VIP
Jr. VIP
Joined
Feb 9, 2013
Messages
319
Reaction score
498
OK, I don't like spoonfeeding so I didn't post earlier but (1) I see that you're still at it and (2) you are not asking for class assignment, so let us make an exception.

Here is how you can do it if you have three programs and want to choose one at random - I just tested this:

Code:
set /a "num=%random%%%3"
if %num% equ 0   goto callpgm1
if %num% equ 1   goto callpgm2
if %num% equ 2   goto callpgm3

:callpgm1
  echo "program1"
  goto myexit
:callpgm2
  echo "program2"
  goto myexit
:callpgm3
  echo "program3"
  goto myexit


:myexit
  pause


Instead of the echo statements, give whatever you want.
 

reuse

Jr. VIP
Jr. VIP
Joined
Feb 9, 2013
Messages
319
Reaction score
498
Because batch is a surefire way to solve things without the need to install anything on a Windows system, and sometimes that really spares a lot of time.

I agree. However, one needs to put in the time needed to get past the learning curve, to reach their desired proficiency level.
 

TiagoS

Senior Member
Joined
Jul 5, 2014
Messages
882
Reaction score
731
Yeah exactly what I wanted to say about batch :p

---

Before I continue, I don't really know much about batch files and the code they use. I can do a little coding in iMacro and even Greasemonkey, but I don't really use batch files.

Once upon a time I needed a way to auto-start many programs when the PC turns on. I went to google, I ended up in windows task scheduler. It was a complicated and slow piece of crap. So then I looked more, and found some freeware program. That worked, but still had issues and was slow, and anyway obviously I had some issue with it because I kept looking. Then I found bat files, and it seemed simple and easy. I figured out how to create a bat file, put it in the Startup folder, and poof it worked and worked well and perfect and continues to work all day every day.

But that's all I know, I don't really understand this code at all, total noob...

So now that I got that out of the way, I can't get this to work. I tried a couple of things:

Code:
@ECHO OFF
set /a num=%random% %%100 ;
IF %num%<50 (start C:\"Programs"\"Program"\Program1.exe)
ELSE (start C:\"Programs"\"Program"\Program1.exe)

Code:
@ECHO OFF
set /a num=%random% %%100
IF %num%<50 (start C:\"Programs"\"Program"\Program1.exe)
ELSE (start C:\"Programs"\"Program"\Program1.exe)

Code:
@ECHO OFF
set /a num=%random% %%100 ;
IF %num%<50 start C:\"Programs"\"Program"\Program1.exe
ELSE start C:\"Programs"\"Program"\Program1.exe

None of these worked. I'm probably making a very noob and obvious mistake, but I don't know this code at all.

This works haha:

Code:
@ECHO OFF
start C:\"Programs"\"Program"\Program1.exe

What's the problem?
Your syntax is the problem :) There's no > in BAT, its GTR, also made some other fixes. I'm using your code, it's not the best way to do it but it seems to work.


Code:
@ECHO OFF
set /a num=%random% %%100
IF %num% GTR 50 ( start C:\"Programs"\"Program"\Program1.exe
) ELSE ( start C:\"Programs"\"Program"\Program2.exe )
 

Scorpion Ghost

Jr. VIP
Jr. VIP
Joined
Mar 22, 2013
Messages
5,454
Reaction score
4,978
Website
smmorigins.com
Your syntax is the problem :) There's no > in BAT, its GTR, also made some other fixes. I'm using your code, it's not the best way to do it but it seems to work.


Code:
@ECHO OFF
set /a num=%random% %%100
IF %num% GTR 50 ( start C:\"Programs"\"Program"\Program1.exe
) ELSE ( start C:\"Programs"\"Program"\Program2.exe )

So this works. But it seems to open Program2 a lot more often than Program1.

Tried once, got 10 times to 1 time.
Second time got 7 times to 1 time.
Third time got 4 times to 1 time.
Fourth time 7 times to 1 time.

It's definitely leaning more to opening Program2 to Program1.

In any case, thanks, again :) but I'd rather have this work for 3 programs rather than two, which brings us to...

OK, I don't like spoonfeeding so I didn't post earlier but (1) I see that you're still at it and (2) you are not asking for class assignment, so let us make an exception.

Here is how you can do it if you have three programs and want to choose one at random - I just tested this:

Code:
set /a "num=%random%%%3"
if %num% equ 0   goto callpgm1
if %num% equ 1   goto callpgm2
if %num% equ 2   goto callpgm3

:callpgm1
  echo "program1"
  goto myexit
:callpgm2
  echo "program2"
  goto myexit
:callpgm3
  echo "program3"
  goto myexit


:myexit
  pause


Instead of the echo statements, give whatever you want.

Many thanks for the spoonfeeding :D

However, it doesn't work (scratch that, scroll down, it worked...) :(

Tried a few things:

Code:
set /a "num=%random%%%3"
if %num% equ 0   goto callpgm1
if %num% equ 1   goto callpgm2
if %num% equ 2   goto callpgm3

:callpgm1
  echo "start C:\"###"\"##"\#.exe"
  goto myexit
:callpgm2
  echo "start C:\"###"\"##"\#.exe"
  goto myexit
:callpgm3
  echo "start C:\"###"\"##"\#.exe"
  goto myexit


:myexit
  pause

Code:
set /a "num=%random%%%3"
if %num% equ 0   goto callpgm1
if %num% equ 1   goto callpgm2
if %num% equ 2   goto callpgm3

:callpgm1
  echo "C:\"###"\"##"\#.exe"
  goto myexit
:callpgm2
  echo "C:\"###"\"##"\#.exe"
  goto myexit
:callpgm3
  echo "C:\"###"\"##"\#.exe"
  goto myexit


:myexit
  pause

Code:
set /a "num=%random%%%3"
if %num% equ 0   goto callpgm1
if %num% equ 1   goto callpgm2
if %num% equ 2   goto callpgm3

:callpgm1
   "start C:\"###"\"##"\#.exe"
  goto myexit
:callpgm2
   "start C:\"###"\"##"\#.exe"
  goto myexit
:callpgm3
   "start C:\"###"\"##"\#.exe"
  goto myexit


:myexit
  pause

Code:
set /a "num=%random%%%3"
if %num% equ 0   goto callpgm1
if %num% equ 1   goto callpgm2
if %num% equ 2   goto callpgm3

:callpgm1
   start C:\"###"\"##"\#.exe
  goto myexit
:callpgm2
   start C:\"###"\"##"\#.exe
  goto myexit
:callpgm3
   start C:\"###"\"##"\#.exe
  goto myexit


:myexit
  pause

Actually, scratch that, the last bit worked. Hooraay! :)

I tried it out, seems to open the programs evenly-ish.

3, 1, 1, 2, 2, 1, 3, 1, 3, 2, 3, 2, 1, 2, 3, 2, 1, 1, 3, 1

There's a test. Ran it 20 times. We have Program One opened 8 times, Program Two opened 6 times, Program Three opened 6 times.

Fantastic, amazing, killer, yeehaa! :D

Thank you, sooooolved! :cool:

Let's song it up in here :D

 

Scorpion Ghost

Jr. VIP
Jr. VIP
Joined
Mar 22, 2013
Messages
5,454
Reaction score
4,978
Website
smmorigins.com
Then go with powershell.

BAT = No go in 2021.

What you say bro? :D

blacksheep-a-black-sheep.png
 

Panther28

Jr. Executive VIP
Jr. VIP
Joined
May 2, 2010
Messages
5,085
Reaction score
7,236
Glad you worked out yourself in the end with obviously some community help.
The trick with any programming language is to try ONE change at a time, and that's what you ended up doing. Kind of like your bloodthirstiness of solving your network problem! :D
 

reuse

Jr. VIP
Jr. VIP
Joined
Feb 9, 2013
Messages
319
Reaction score
498
Many thanks for the spoonfeeding :D

However, it doesn't work (scratch that, scroll down, it worked...) :(

Tried a few things:

Code:
set /a "num=%random%%%3"
if %num% equ 0   goto callpgm1
if %num% equ 1   goto callpgm2
if %num% equ 2   goto callpgm3

:callpgm1
  echo "start C:\"###"\"##"\#.exe"
  goto myexit
:callpgm2
  echo "start C:\"###"\"##"\#.exe"
  goto myexit
:callpgm3
  echo "start C:\"###"\"##"\#.exe"
  goto myexit


:myexit
  pause

Code:
set /a "num=%random%%%3"
if %num% equ 0   goto callpgm1
if %num% equ 1   goto callpgm2
if %num% equ 2   goto callpgm3

:callpgm1
  echo "C:\"###"\"##"\#.exe"
  goto myexit
:callpgm2
  echo "C:\"###"\"##"\#.exe"
  goto myexit
:callpgm3
  echo "C:\"###"\"##"\#.exe"
  goto myexit


:myexit
  pause

Code:
set /a "num=%random%%%3"
if %num% equ 0   goto callpgm1
if %num% equ 1   goto callpgm2
if %num% equ 2   goto callpgm3

:callpgm1
   "start C:\"###"\"##"\#.exe"
  goto myexit
:callpgm2
   "start C:\"###"\"##"\#.exe"
  goto myexit
:callpgm3
   "start C:\"###"\"##"\#.exe"
  goto myexit


:myexit
  pause

Code:
set /a "num=%random%%%3"
if %num% equ 0   goto callpgm1
if %num% equ 1   goto callpgm2
if %num% equ 2   goto callpgm3

:callpgm1
   start C:\"###"\"##"\#.exe
  goto myexit
:callpgm2
   start C:\"###"\"##"\#.exe
  goto myexit
:callpgm3
   start C:\"###"\"##"\#.exe
  goto myexit


:myexit
  pause

Actually, scratch that, the last bit worked. Hooraay! :)

I tried it out, seems to open the programs evenly-ish.

3, 1, 1, 2, 2, 1, 3, 1, 3, 2, 3, 2, 1, 2, 3, 2, 1, 1, 3, 1

Yes, most of the top voted answers at StackOverflow open them unevenly, hence I wrote that customized for your situation.

I had written this along with my answer "Instead of the echo statements, give whatever you want."
That's what you did eventually, by replacing the echo statement with your program call:
start C:\"###"\"##"\#.exe

You really kept at it, instead of abandoning, so kudos to that. Persistence wins.

I was looking for a good link for you, without ads. Found a couple:

Those were probably written back in 1990's or 2000's. It is surprising how little batch syntax has changed since then.

Good luck.
 

Scorpion Ghost

Jr. VIP
Jr. VIP
Joined
Mar 22, 2013
Messages
5,454
Reaction score
4,978
Website
smmorigins.com
Glad you worked out yourself in the end with obviously some community help.
The trick with any programming language is to try ONE change at a time, and that's what you ended up doing. Kind of like your bloodthirstiness of solving your network problem! :D

I thirst, for blood! :D

Yeah all good here, problem solved. All is well that ends well :)

Yes, most of the top voted answers at StackOverflow open them unevenly, hence I wrote that customized for your situation.

I had written this along with my answer "Instead of the echo statements, give whatever you want."
That's what you did eventually, by replacing the echo statement with your program call:
start C:\"###"\"##"\#.exe

You really kept at it, instead of abandoning, so kudos to that. Persistence wins.

I was looking for a good link for you, without ads. Found a couple:

Those were probably written back in 1990's or 2000's. It is surprising how little batch syntax has changed since then.

Good luck.

Yeah man, you hooked me up. What you wrote works perfectly. It's exactly what I wanted. Thanks again :)
 

TiagoS

Senior Member
Joined
Jul 5, 2014
Messages
882
Reaction score
731
So this works. But it seems to open Program2 a lot more often than Program1.

Tried once, got 10 times to 1 time.
Second time got 7 times to 1 time.
Third time got 4 times to 1 time.
Fourth time 7 times to 1 time.

It's definitely leaning more to opening Program2 to Program1.

In any case, thanks, again :) but I'd rather have this work for 3 programs rather than two, which brings us to...



Many thanks for the spoonfeeding :D

However, it doesn't work (scratch that, scroll down, it worked...) :(

Tried a few things:

Code:
set /a "num=%random%%%3"
if %num% equ 0   goto callpgm1
if %num% equ 1   goto callpgm2
if %num% equ 2   goto callpgm3

:callpgm1
  echo "start C:\"###"\"##"\#.exe"
  goto myexit
:callpgm2
  echo "start C:\"###"\"##"\#.exe"
  goto myexit
:callpgm3
  echo "start C:\"###"\"##"\#.exe"
  goto myexit


:myexit
  pause

Code:
set /a "num=%random%%%3"
if %num% equ 0   goto callpgm1
if %num% equ 1   goto callpgm2
if %num% equ 2   goto callpgm3

:callpgm1
  echo "C:\"###"\"##"\#.exe"
  goto myexit
:callpgm2
  echo "C:\"###"\"##"\#.exe"
  goto myexit
:callpgm3
  echo "C:\"###"\"##"\#.exe"
  goto myexit


:myexit
  pause

Code:
set /a "num=%random%%%3"
if %num% equ 0   goto callpgm1
if %num% equ 1   goto callpgm2
if %num% equ 2   goto callpgm3

:callpgm1
   "start C:\"###"\"##"\#.exe"
  goto myexit
:callpgm2
   "start C:\"###"\"##"\#.exe"
  goto myexit
:callpgm3
   "start C:\"###"\"##"\#.exe"
  goto myexit


:myexit
  pause

Code:
set /a "num=%random%%%3"
if %num% equ 0   goto callpgm1
if %num% equ 1   goto callpgm2
if %num% equ 2   goto callpgm3

:callpgm1
   start C:\"###"\"##"\#.exe
  goto myexit
:callpgm2
   start C:\"###"\"##"\#.exe
  goto myexit
:callpgm3
   start C:\"###"\"##"\#.exe
  goto myexit


:myexit
  pause

Actually, scratch that, the last bit worked. Hooraay! :)

I tried it out, seems to open the programs evenly-ish.

3, 1, 1, 2, 2, 1, 3, 1, 3, 2, 3, 2, 1, 2, 3, 2, 1, 1, 3, 1

There's a test. Ran it 20 times. We have Program One opened 8 times, Program Two opened 6 times, Program Three opened 6 times.

Fantastic, amazing, killer, yeehaa! :D

Thank you, sooooolved! :cool:

Let's song it up in here :D

Well yes, it will tend to pick the higher one due to the nature of how this method works. I don't have the full explanation but you can look it up.
 
Top