Page 1 of 1

If file is exited

Posted: 07 Jun 2010 19:32
by sp11k3t3ht3rd
Is there a way to check if a batch file is closed out of?

Re: If file is exited

Posted: 08 Jun 2010 08:55
by !k
sp11k3t3ht3rd
Ex.

Code: Select all

@echo off
for /f "tokens=1" %%a in ('tasklist /nh /fi "windowtitle eq My Cool Cmd Window Title" 2^>nul') do (
if "%%a"=="cmd.exe" echo Running
)

Re: If file is exited

Posted: 08 Jun 2010 10:41
by aGerman
example.bat

Code: Select all

@echo off
title EXAMPLE
pause




check.bat

Code: Select all

@echo off

REM loop while example.bat is not running
:loop1
tasklist /nh /fi "windowtitle eq EXAMPLE" 2>nul |findstr /l "cmd.exe" >nul || (
  ping -n 2 localhost>nul
  goto :loop1
)

REM loop while example.bat is running
:loop2
tasklist /nh /fi "windowtitle eq EXAMPLE" 2>nul |findstr /l "cmd.exe" >nul && (
  ping -n 2 localhost>nul
  goto :loop2
)

REM display a message
echo example.bat is closed
echo.
pause




Run both batch files, after that close example.bat to display a message on the check.bat window.

Regards
aGerman

Re: If file is exited

Posted: 10 Jun 2010 14:11
by sp11k3t3ht3rd
aGerman wrote:example.bat

Code: Select all

@echo off
title EXAMPLE
pause




check.bat

Code: Select all

@echo off

REM loop while example.bat is not running
:loop1
tasklist /nh /fi "windowtitle eq EXAMPLE" 2>nul |findstr /l "cmd.exe" >nul || (
  ping -n 2 localhost>nul
  goto :loop1
)

REM loop while example.bat is running
:loop2
tasklist /nh /fi "windowtitle eq EXAMPLE" 2>nul |findstr /l "cmd.exe" >nul && (
  ping -n 2 localhost>nul
  goto :loop2
)

REM display a message
echo example.bat is closed
echo.
pause




Run both batch files, after that close example.bat to display a message on the check.bat window.

Regards
aGerman

Is there anyway to minimized the check.bat once it starts?

Re: If file is exited

Posted: 10 Jun 2010 15:50
by aGerman
sp11k3t3ht3rd wrote:Is there anyway to minimized the check.bat once it starts?


You could start it using a link in a minimized window or you could use this as the first line of your batch code:

Code: Select all

@echo off&setlocal&@set "tmpVariable="||(set "tmpVariable=1"&start "%~dpnx0" /min cmd /c %0 %*&set "tmpVariable="&goto :eof)


The batch will restart itself in a minimized window.

But imo this makes no sense, because you will not realize the message.

Regards
aGerman

Re: If file is exited

Posted: 10 Jun 2010 16:12
by sp11k3t3ht3rd
I wouldn't need to see the message with this modified code...

Code: Select all

@echo off
title CHECKER1

set /p op=Type the file name you want to be tested to run. (Omit the file extension and   make sure the batch file title is the name you enter.):
REM loop while %op%.bat is not running
:loop1
tasklist /nh /fi "windowtitle eq %op%" 2>nul |findstr /l "cmd.exe" >nul || (
   ping -n 2 localhost>nul
   echo %op%.bat is not running >>%op%" "log.txt
   goto :loop1
)

REM loop while %op%.bat is running
:loop2
tasklist /nh /fi "windowtitle eq %op%" 2>nul |findstr /l "cmd.exe" >nul && (
   ping -n 2 localhost>nul
   echo %op%.bat is running >>%op%" "log.txt
   goto :loop2
)

REM display a message
echo %op%.bat is closed >>%op%" "log.txt
pause


but another question...is there any way so I could have it print out the date each time? Like lets say you have a file log already to separate each time it was used is there a way to have it print out something like "[June 10th 6:11 P.M.]"?

Re: If file is exited

Posted: 10 Jun 2010 16:24
by aGerman
cmd.exe itself sets the current date and time to variables. So you can use %date% and %time% for your echo-redirection without defining them before.

Regards
aGerman

Re: If file is exited

Posted: 10 Jun 2010 16:31
by sp11k3t3ht3rd
Thanks!!!!!!!!!!! :D