Friday, September 17, 2010

Batch scripting

Using a batch script to determine if a process was still running.  Designed on Win7.

rem This code enables a controlled exit to properly report to the package installation process.
rem Dave Fanning

:: This is a batch file comment

SET TIMECMD=time /T

rem set start minute value
FOR /F "tokens=2 delims=:" %%A IN ('"%TIMECMD%"') DO SET STARTTIME=%%A
echo Waiting for cataloging to complete.
rem using ping to sleep
ping -n 2 127.0.0.1 > NUL

:CHECK

rem checking to see if db2.exe has finished running
FOR /F "usebackq tokens=2,3" %%C IN (`tasklist /FI "IMAGENAME eq db2.exe"`) DO SET DB2CHECK=%%C


IF %DB2CHECK%==No (GOTO:SUCCESS)


rem find time now
FOR /F "tokens=2 delims=:" %%B IN ('"%TIMECMD%"') DO SET TIMENOW=%%B

SET /A TIMEDIFF=%TIMENOW%-%STARTTIME%

rem check that the loop has been running less than 2 minutes
IF %TIMEDIFF% LSS 2 (GOTO:CHECK) ELSE (GOTO:TIMEOUT)


:TIMEOUT
rem the db2.exe did not finish operations within maximum 2 minutes.
rem end with the errorlevel 1
exit 1


:SUCCESS
rem OK the process terminated within 2 minutes
exit 0





This one just checks if the process is finished before continuing

rem wait for installer.exe to complete before moving the lnk file
:CHECK
rem waiting 20 secs
ping -n 20 127.0.0.1 > NUL
rem checking to see if installer.exe has finished running
FOR /F "usebackq tokens=2,3" %%C IN (`tasklist /FI "IMAGENAME eq installer.exe"`) DO SET INSTCHECK=%%C

IF NOT %INSTCHECK%==No (GOTO:CHECK)

Next command goes here.




A standard tasklist command to check for a partiular process could be

tasklist /FI "IMAGENAME eq Exc*"

this would return all process matching Exc* e.g. Excel.exe