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