I'm trying to create a batch file to run a program at random, but it does not work how I can make batch file work?
set /a timeout=%RANDOM% * 21 / 32768 + 20
timeout %timeout%
notepad.exe
I'm trying to create a batch file to run a program at random, but it does not work how I can make batch file work?
set /a timeout=%RANDOM% * 21 / 32768 + 20
timeout %timeout%
notepad.exe
Your description of the problem is unclear. it is not working" can mean anything. The code you posted works. and does exactly what you asked it to do:
set /a timeout=%RANDOM% * 21 / 32768 + 20
timeout %timeout%
notepad.exe
Based on you comment however, "this script runs only every 20 seconds the program should do it randomly at any second" I am assuming you want to loop the code, like this:
:loop
set /a timeout=%RANDOM% * 21 / 32768 + 20
timeout %timeout%
notepad.exe
goto :loop
That however is dangerous, if you open notepad
too many times (especially on low memory devices), the pc can become unresponsive. So instead, do a process count and only start new instances if in the number of processes are not met:
:loop
set /a timeout=%RANDOM% * 21 / 32768 + 20 & timeout %timeout%
for /f "tokens=1,*" %%i in ('tasklist ^| find /I /C "notepad.exe"') do set var=%%i
if %var% LSS 10 start notepad.exe
goto :loop
This example will loop endlessly, but it will only start notepad.exe
if 10 or less instances are running. This means a maximum of 10 Notepad sessions allowed. The number after LSS
can be increased/decreased.