Coding help!

CoTTon

Registered User
Joined
Jun 11, 2011
Messages
964
Hey guys

so I currently have this :
Code:
<code>setlocal enableextensions enabledelayedexpansion
rem what to open
set "_process=notepad.exe"
set "_keep=4"

rem infinite loop: from 0 to 1 in steps of 0
for /l %%a in (0 0 1) do (
    rem get count of started processes
    for /f %%b in ('tasklist ^| find /c /i "%_process%"') do if %%b LSS %_keep% (
        rem start as many processes as needed
        set /a _started=%%b+1
        for /l %%c in (!_started! 1 %_keep%) do (
            start "" "%_process%"
        )
    )
    rem wait
    timeout /t 3 > nul
)</code>

I want to be able to have a slight delay - when it opens each program back up - eg: 10 seconds each process

EG: opens 4 - have 3 crash - so the script will reopen 3 back, but then there is 10 seconds delay between each program being reopened.
 
If you just want it to have a delay between spawning each process, the fix is rather simple.

Code:
setlocal enableextensions enabledelayedexpansion
rem what to open
set "_process=notepad.exe"
set "_keep=4"

rem infinite loop: from 0 to 1 in steps of 0
for /l %%a in (0 0 1) do (
    rem get count of started processes
    for /f %%b in ('tasklist ^| find /c /i "%_process%"') do if %%b LSS %_keep% (
        rem start as many processes as needed
        set /a _started=%%b+1
        for /l %%c in (!_started! 1 %_keep%) do (
            start "" "%_process%"
            timeout /t 10 > nul
        )
    )
    rem wait
    timeout /t 3 > nul
)

If you want to have a delay when re-opening processes (so all spawns of a process excluding the first), then that's a bit more complex. The easiest way to achieve that would be to keep a variable firstState. It defaults to true, you do your first launches (without any delay), and then you set firstState to false. In your launch code have something along the following lines

if (!firstState)
timeout /t 10 > nul
 
Thx. Will try it when I'm back home.

Im thinking for the first time it opens 4 processes I can create another batch file to open them with a delay.

Then have this script open after the first one finishes
 
Thx. Will try it when I'm back home.

Im thinking for the first time it opens 4 processes I can create another batch file to open them with a delay.

Then have this script open after the first one finishes

Here's code that will do the first four instantly, then all subsequent loads are delayed by 10 seconds.

Code:
setlocal enableextensions enabledelayedexpansion
rem what to open
set "_process=notepad.exe"
set "_keep=4"
set /a _runCount=0

rem infinite loop: from 0 to 1 in steps of 0
for /l %%a in (0 0 1) do (
    rem get count of started processes
    for /f %%b in ('tasklist ^| find /c /i "%_process%"') do if %%b LSS %_keep% (
        rem start as many processes as needed
        set /a _started=%%b+1
        for /l %%c in (!_started! 1 %_keep%) do (
            start "" "%_process%"
            if !_runCount! GTR %_keep% (timeout /t 10 > nul)
            set /a _runCount=_runCount+1
        )
    )
    rem wait
    timeout /t 3 > nul
)
 
Yep - this works perfectly.

Thanks -

And thanks for registering today and posting :)

I created a batch file where the first time it opens notepad it will delay by 10 secs before opening -

after batch file is done it will open this script - and monitor and reopen if one closes

this was the script which does it -
Code:
setlocal enableextensions enabledelayedexpansion
rem what to open
set "_process=notepad.exe"
set "_keep=4"

rem infinite loop: from 0 to 1 in steps of 0
for /l %%a in (0 0 1) do (
    rem get count of started processes
    for /f %%b in ('tasklist ^| find /c /i "%_process%"') do if %%b LSS %_keep% (
        rem start as many processes as needed
        set /a _started=%%b+1
        for /l %%c in (!_started! 1 %_keep%) do (
            start "" "%_process%"
            timeout /t 10 > nul
        )
    )
    rem wait
    timeout /t 3 > nul
)

so currently have 2 files

Code:
start notepad
ping localhost -n 3 > nul

start notepad
ping localhost -n 3 > nul

start batch2.bat

or... i dont even really need to open any note pads as this thing will see there is none open and open one at a time :)
 
Back
Top