Open multiple applications (if they are not already open)?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jerbot
Posts: 2
Joined: 14 Apr 2014 18:34

Open multiple applications (if they are not already open)?

#1 Post by Jerbot » 14 Apr 2014 18:43

Dear Dostips,

I have found various different scripts and methods by which to check whether or not an application is running and open it if it is not running. Most of them end the batch file's operation if one of the applications is already running, though. Also, I can't tell what are keywords and what are variable names, sometimes. So, I'd like to start from scratch. Can you help me? I'll walk you through the logic, first.


This is what I imagine.

Code: Select all

echo Hello, sir.
function: checkApp1
  Check if app1 is running.
  If app1 is running, continue to function checkApp2.
  elseIf app1 is not running, c:\app1\app1.ese
function: checkApp2
  Check if app2 is running.
  If app2 is running, continue to function focusApp1.
  elseIf app2 is not running, c:\app1\app2.ese
function: focusApp1
  Bring app1 to the focus.
echo Thank you, sir.


Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Open multiple applications (if they are not already open

#2 Post by Ed Dyreen » 14 Apr 2014 20:28

Code: Select all

@echo off &setlocal enableDelayedExpansion
:: main
:: (
   echo Hello, sir.

   echo. Check if app1 is running.
   ::call :processExists "checkApp1" >nul
   call :processExists "notepad.exe" >nul &if not errorLevel 1 (

      echo. If app1 is running, continue to function checkApp2.
      echo. Check if app2 is running.
      echo. If app2 is running, continue to function focusApp1.
      :: call :winGetProcess app2.windowName "app2"
      call :winGetProcess app2.windowName "doskit" &if not errorLevel 1 (

         echo. Bring app1 to the focus.
         if not exist cmdow.EXE (

            echo. google and download cmdow.EXE first ^^^!
            pause
            exit 1
         )
         cmdow.EXE /ACT "!app2.windowName!"

      ) else (

         echo. elseIf app2 is not running, c:\app1\app2.ese
         start "app2" /WAIT "c:\app1\app2.eXe"
      )

   ) else (

      echo. elseIf app1 is not running, c:\app1\app1.ese
      start "app1" /WAIT "c:\app1\app1.eXe"
   )

   echo Thank you, sir.
   pause
:: )
endlocal &exit

:processExists processName
:: (
   set "p=%~1"
   taskList.EXE /nh |findStr.EXE /bric:"!p:~0,25!" &&set /a $err = 0 ||set /a $err = 1
:: )
exit /b !$err!

:winGetProcess windowName processName
:: (
   set "w=%~2"

   set /a $err = 1 &set "_=" &for /f "delims=" %%? in (

      'taskList/nh /fi "windowTitle eq !w!*"'

   ) do set /a $err = 0 &set "_=%%?" &set _=!_:~0,25!

   set "%~1=!_!"
:: )
exit /b !$err!

pause
exit

Code: Select all

Hello, sir.
 Check if app1 is running.
 If app1 is running, continue to function checkApp2.
 Check if app2 is running.
 If app2 is running, continue to function focusApp1.
 Bring app1 to the focus.
 google and download cmdow.EXE first !
Druk op een toets om door te gaan. . .

Jerbot
Posts: 2
Joined: 14 Apr 2014 18:34

Re: Open multiple applications (if they are not already open

#3 Post by Jerbot » 14 Apr 2014 21:27

I'm going to try to reorganize it and ask questions.


::Header?
@echo off &setlocal enableDelayedExpansion

::Function definitions before main?

:processExists processName
:: (
::Can you explain these lines?
set "p=%~1"
taskList.EXE /nh |findStr.EXE /bric:"!p:~0,25!" &&set /a $err = 0 ||set /a $err = 1
:: )
exit /b !$err!


::are "windowName and processName" args?
:winGetProcess windowName processName
:: (
set "w=%~2"
::Can you explain this line?
set /a $err = 1 &set "_=" &for /f "delims=" %%? in (

'taskList/nh /fi "windowTitle eq !w!*"'

) do set /a $err = 0 &set "_=%%?" &set _=!_:~0,25!

set "%~1=!_!"
:: )
exit /b !$err!


:: main
:: (
echo Hello, sir.

echo. Is cmd running?
::format: call :processExists "processName" >nul
::where does errorLevel 1 lead? Should I contain capsulize errorLevel? Is errorLevel a built-in function, or is it being defined and run here?
call :processExists "cmd.exe" >nul &if not errorLevel 1 (

echo. If app1 is already running, continue to function checkApp2.
::format: call :winGetProcess app2.windowName "app2"
::Why are we using a different method of discovering whether or not an app is running? Is locating the window name the best method? Programs like notepad++ have the same process name, but the window name is variable.
call :winGetProcess app2.windowName "notepad++.exe" &if not errorLevel 1 (

::Do I actually need to install cmdow to focus a window?
echo. Bring app1 to the focus.

if not exist cmdow.EXE (
echo. google and download cmdow.EXE first ^^^!
pause
exit 1
)
cmdow.EXE /ACT "!app2.windowName!"

) else (
echo. elseIf app2 is not running, c:\app1\app2.ese
::There's a start command? Is that part of cmdow?
start "app2" /WAIT "c:\app1\app2.eXe"
)

) else (
echo. elseIf app1 is not running, c:\app1\app1.ese
start "app1" /WAIT "c:\app1\app1.eXe"
)

echo Thank you, sir.
pause
:: )
endlocal &exit


pause
exit

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Open multiple applications (if they are not already open

#4 Post by Ed Dyreen » 14 Apr 2014 23:45

Jerbot wrote:I'm going to try to reorganize it and ask questions.


::Header?
@echo off &setlocal enableDelayedExpansion
Not, exactly. echo off prevent automatic printing of commands in the console and delayed expansion allows delayed expansion of variables, that is at execution time.
Jerbot wrote:::Function definitions before main?
No no, the double colons :: indicate a comment so :: main is just a comment.
Jerbot wrote::processExists processName
:: (
::Can you explain these lines?
set "p=%~1"
taskList.EXE /nh |findStr.EXE /bric:"!p:~0,25!" &&set /a $err = 0 ||set /a $err = 1
Just type the programs and append /?, eg; taskList.EXE /? all the switches will be explained. Furthermore && means execute the next command if the previous exited with errorlevel 0, || means the opposite.
Jerbot wrote::: )
exit /b !$err!


::are "windowName and processName" args?
You certainly have a background in programming but no, DOS is not that smart. Again just a comment, the args are accessed with %~1 %~2 etc...
Jerbot wrote::winGetProcess windowName processName
:: (
set "w=%~2"
::Can you explain this line?
type for /? in the console, the help on this command is quite extensive, so is it's use. Simply put, it is the DOS way to delimit strings.
Jerbot wrote: set /a $err = 1 &set "_=" &for /f "delims=" %%? in (

'taskList/nh /fi "windowTitle eq !w!*"'

) do set /a $err = 0 &set "_=%%?" &set _=!_:~0,25!

set "%~1=!_!"
:: )
exit /b !$err!

:: main
:: (
echo Hello, sir.

echo. Is cmd running?
::format: call :processExists "processName" >nul
::where does errorLevel 1 lead? Should I contain capsulize errorLevel? Is errorLevel a built-in function, or is it being defined and run here?
if not errorlevel 1 is the only way to check for errorlevel 0 in batch. It's an internal command yes. What you should never do is set the errorLevel manually because that will prevent DOS from setting the errorlevel automatically and most commands do set the errorlevel.
Jerbot wrote: call :processExists "cmd.exe" >nul &if not errorLevel 1 (

echo. If app1 is already running, continue to function checkApp2.
::format: call :winGetProcess app2.windowName "app2"
::Why are we using a different method of discovering whether or not an app is running? Is locating the window name the best method? Programs like notepad++ have the same process name, but the window name is variable.
You need tell cmdow which window needs focus, this can be a winHandle or it's caption. Caption's are not reliable but handle's are not supported by tasklist.EXE because taskList.EXE is a DOS program and has no notion of windows.
Jerbot wrote: call :winGetProcess app2.windowName "notepad++.exe" &if not errorLevel 1 (

::Do I actually need to install cmdow to focus a window?
installation is nothing more than downloading the program and placing it in your script's directory.
Jerbot wrote: echo. Bring app1 to the focus.

if not exist cmdow.EXE (
echo. google and download cmdow.EXE first ^^^!
pause
exit 1
)
cmdow.EXE /ACT "!app2.windowName!"

) else (
echo. elseIf app2 is not running, c:\app1\app2.ese
::There's a start command? Is that part of cmdow?
Another internal command but not required, you could also just write "c:\app1\app2.eXe" but if the program can't be found, the interpreter won't pause and report the problem in a GUI.
Jerbot wrote: start "app2" /WAIT "c:\app1\app2.eXe"
)

) else (
echo. elseIf app1 is not running, c:\app1\app1.ese
start "app1" /WAIT "c:\app1\app1.eXe"
)

echo Thank you, sir.
pause
:: )
endlocal &exit


pause
exit

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Open multiple applications (if they are not already open

#5 Post by foxidrive » 14 Apr 2014 23:50

Ed Dyreen wrote:if not errorlevel 1 is the only way to check for errorlevel 0 in batch. It's an internal command yes.


You probably meant something a little different - but there are other ways to test for errorlevel 0

if %errorlevel%==0
if %errorlevel% EQU 0


and also using delayed expansion.

if not errorlevel 1 is a perfectly good way though.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Open multiple applications (if they are not already open

#6 Post by Ed Dyreen » 15 Apr 2014 00:08

And now I realize I messed up, I didn't need winGetProcess but processGetCaption :P

Sorry :D

Anyways what you'll need is something like this ( though retrieving the handle is much nicer )

Code: Select all

set i=&for /f "delims=" %%? in (
'taskList /nh /v /fo csv /fi "imageName eq !p!"2^^^>nul'
) do (
Now you try :wink:

Post Reply