If file is exited

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

If file is exited

#1 Post by sp11k3t3ht3rd » 07 Jun 2010 19:32

Is there a way to check if a batch file is closed out of?

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: If file is exited

#2 Post by !k » 08 Jun 2010 08:55

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
)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: If file is exited

#3 Post by aGerman » 08 Jun 2010 10:41

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

sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

Re: If file is exited

#4 Post by sp11k3t3ht3rd » 10 Jun 2010 14:11

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?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: If file is exited

#5 Post by aGerman » 10 Jun 2010 15:50

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

sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

Re: If file is exited

#6 Post by sp11k3t3ht3rd » 10 Jun 2010 16:12

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.]"?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: If file is exited

#7 Post by aGerman » 10 Jun 2010 16:24

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

sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

Re: If file is exited

#8 Post by sp11k3t3ht3rd » 10 Jun 2010 16:31

Thanks!!!!!!!!!!! :D

Post Reply