Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following simple scheme seems to work. The cleanup script calls the main script via the START command with the /WAIT option. This ensures control will return even if the child process is killed by closing the window.</p> <p>The parent cleanup script will normally prompt if you want to <em>"Terminate batch job (Y/N)?"</em> if the main script is killed by closing the window. I pipe N into the START command to automatically answer that prompt, should it arise. The outer block redirection of stderr to nul suppresses the ugly <code>^C</code> (Control-C) that shows up when the main window is killed.</p> <p>The main batch file isn't STARTed directly, but instead is launched via the CMD /C command. This ensures control will return even if the main script is killed by pressing Control-C. This also eliminates the need to put an EXIT command in the main script (which might never be executed any way).</p> <p><strong>cleanup.bat</strong></p> <pre><code>@echo off :: Launch the main program and don't continue until it completes (or is killed) 2&gt;nul ( echo N|start /wait "" cmd /c main.bat ) :: Cleanup up begins here echo Time to wrap it up! </code></pre> <p>Here is a trivial <strong>main.bat</strong> to test the return functionality</p> <pre><code>@echo off echo This is the main script. echo( echo Press any key to close normally echo or Close this window via the console window X button echo or Press Ctrl-C to break out of this script abnormally pause &gt;nul </code></pre> <p><br/><strong><em>EDIT</em></strong></p> <p>Of course there is nothing to prevent a user from closing the "cleanup.bat" window before the main script terminates, which puts you right back to square one :-(</p> <p>You can run a VBScript or Jscript via wscript without any visible window to close. The (VB/J)script can then launch the main.bat script and then launch a cleanup.bat script only when the main process finishes (or is killed). That would make it more difficult for an end user to interfere with the process of kicking off the cleanup script.</p> <p><strong>master.vbs</strong></p> <pre><code>Set WshShell = CreateObject("WScript.Shell") WshShell.Run "D:\test\main.bat", 1, TRUE WshShell.Run "D:\test\cleanup.bat", 1, TRUE Set WshShell = Nothing </code></pre> <p>But once you cross that threshould, why not do the entire project in (VB/J)script?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload