Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8293639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:53:50+00:00 2026-06-08T13:53:50+00:00

I need to run a batch Script A and this is only run either

  • 0

I need to run a batch “Script A” and this is only run either when the batch is closed programatically via the “Do you want to exit [y / n] questions” or when all the scripts within the batch are concluded.

If anytime the user decides to close the batch via the mouse button (“X” on the upper right-hand side of the console) of if the batch crashes, “Script A” will, unfortunatelly, not be executed.

How can I programatically intercept any moment of “X” and launch another batch to finalize the deed?

Kind regards,

Maleck

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:53:52+00:00Added an answer on June 8, 2026 at 1:53 pm

    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.

    The parent cleanup script will normally prompt if you want to “Terminate batch job (Y/N)?” 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 ^C (Control-C) that shows up when the main window is killed.

    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).

    cleanup.bat

    @echo off
    :: Launch the main program and don't continue until it completes (or is killed)
    2>nul (
      echo N|start /wait "" cmd /c main.bat
    )
    
    :: Cleanup up begins here
    echo Time to wrap it up!
    

    Here is a trivial main.bat to test the return functionality

    @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 >nul
    

    EDIT

    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 🙁

    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.

    master.vbs

    Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run "D:\test\main.bat", 1, TRUE
    WshShell.Run "D:\test\cleanup.bat", 1, TRUE
    Set WshShell = Nothing
    

    But once you cross that threshould, why not do the entire project in (VB/J)script?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run a batch file via VB and I need to wait
I need to run a .cmd batch file from within a php script. The
I want to start a batch-script in my eclipse. (Instructions here: How to run
I need to run a script that will generate the projects.list file when a
I need to run a script and have access to the default stdin (terminal
I need to run a standalone ruby script as Unix (linux) daemon. After running
I am trying to run a script ( MouseParty.pie ) using Glovepie . This
I have a batch file that I need to run within my NSIS installer.
First of all, this may need to be moved to superuser. I couldn't decide
I am attempting to run a Windows batch script nightly to pull a fresh

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.