If file is exited
Moderator: DosItHelp
-
- Posts: 26
- Joined: 20 May 2010 15:39
If file is exited
Is there a way to check if a batch file is closed out of?
Re: If file is exited
sp11k3t3ht3rd
Ex.
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
example.bat
check.bat
Run both batch files, after that close example.bat to display a message on the check.bat window.
Regards
aGerman
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
-
- Posts: 26
- Joined: 20 May 2010 15:39
Re: If file is exited
aGerman wrote:example.batCode: Select all
@echo off
title EXAMPLE
pause
check.batCode: 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
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
-
- Posts: 26
- Joined: 20 May 2010 15:39
Re: If file is exited
I wouldn't need to see the message with this modified code...
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.]"?
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
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
Regards
aGerman