How to close a bat after a specific time without intrerupting the execution?

Marko

I want to manage to execute and manipulate my bat file, but, în background i want a countdown so that after a specific time, no matter what, my bat calls another one that deletes it, or simply closes.

Also i want the bat file to show up on execution maximized. I have set /max after start command, but still minimized. I used în my bat /min and it worked, cant figure out why /max doesent work.

Dennis van Gils

To make it start maximized put this at the top of your script, below @echo off

if not "%1" == "max" start /MAX cmd /c %0 max & exit/b

I'll update this answer later with the other part, but I think opening a new batch with a timeout as soon as this one starts with start /B should help a lot.

EDIT

So this starts your script with a second script in it. That second script kills the first script and starts a third cmd to delete itself:

@echo off
if not "%1" == "max" start /MAX cmd /c %0 max & exit/b
set T=%TEMP%\sthUnique.tmp
wmic process where (Name="WMIC.exe" AND CommandLine LIKE "%%%TIME%%%") get ParentProcessId /value | find "ParentProcessId" >%T%
set /P A=<%T%
SET PID=%A:~16%
echo @echo off > killParent.bat
:: replace the 5 on the next line with any other number
echo timeout /t 5 /nobreak >> killParent.bat
echo start "" cmd ^/c timeout ^/t 1^^^&del "%%~f0"^^^&exit ^/b >> killParent.bat
echo taskkill /f /PID %PID% >> killParent.bat
START /B CMD /C CALL killParent.bat >NUL 2>&1
::any logic here instead of pause
pause

Place your code where the pause is and replace the 5 with the number of seconds you want to wait.

This does have the drawback of not being able to finish before the timer runs out, you can fix that by putting echo title parentKiller >> killParent.bat below echo @echo off > killParent.bat and putting

del killParent.bat
taskkill /F /FI "WINDOWTITLE eq parentKiller *" /T

at the end of your execution path, so at the bottom of your batch file normally. This would then look like this:

@echo off
if not "%1" == "max" start /MAX cmd /c %0 max & exit/b
set T=%TEMP%\sthUnique.tmp
wmic process where (Name="WMIC.exe" AND CommandLine LIKE "%%%TIME%%%") get ParentProcessId /value | find "ParentProcessId" >%T%
set /P A=<%T%
SET PID=%A:~16%
echo @echo off > killParent.bat
echo title parentKiller >> killParent.bat
:: replace the 5 on the next line with any other number
echo timeout /t 5 /nobreak >> killParent.bat
echo start "" cmd ^/c timeout ^/t 1^^^&del "%%~f0"^^^&exit ^/b >> killParent.bat
echo taskkill /f /PID %PID% >> killParent.bat
START /B CMD /C CALL killParent.bat >NUL 2>&1
::any logic here instead of pause
pause
del killParent.bat
taskkill /F /FI "WINDOWTITLE eq parentKiller *" /T

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to close a specific java window with a bat?

How to make kivy window close automatically after specific amount of time?

How to automatically close an Interstitial Ad after specific amount of time?

how to measure execution time in specific part of View

How to stop execution after a certain time in Java?

Batch file execution with closing automatically after a specific period of time

JS Function self-disable for a specific time after execution

How can I change a label text after a specific time without after() function in tkinter python?

How to automatically close cmd window after batch file execution?

How to automatically close the execution of the `qemu` after end of process?

How to close the cmd prompt window automatically after a batch file execution

How to get execution time of a specific script that is running, but in the background?

How to close specific JFrame based on events on JPanel without exiting application?

How to list files without directories with specific time

How to print server execution time and roundup time after query execution in RethinkDB?

How to avoid concurrent execution of a time-consuming task without blocking?

How to close and rerun a python exe after every 5min using .bat file

How to use the MongoClient without coding "client.close()" every time?

how to close browser after execution of all testcase or after fail of testcase with protractor using selenium server with angularjs

How is execution resumed after a hardware breakpoint without an infinite loop?

How to stop execution a Flux after a certain time in Java?

Plotly / Dash - Python how to stop execution after time?

How to implement the time.Sleep after the template execution?

How to keep running a Thread after specific time

How to interrupt a method after specific time in Java?

How to remove SKSpriteNodes after specific amount of time

how to get docs after specific date time?

how to fire a function after hovering for a specific time?

how to reload table view after a specific time