user22277
user22277

Reputation: 117

Detecting when a program is closed in command prompt

I have a code that allows me to change the system date when I launch a program. I want my code to change the date when I close said program.

set before=%date%
echo 01/06/2012 | date
start C:\Skittles\Rainbows3.1\angels.cmd
timeout 5
echo %before% | date

so instead of changing the date right away (lines 4-5 of my code), I want it to change when the program closes

hope this was clear enough.

Upvotes: 0

Views: 2967

Answers (1)

Harry Johnston
Harry Johnston

Reputation: 36318

OK, so I did some tests, and it turns out the behaviour of the start command when applied to a batch script is not what I thought it was.

It turns out that

start anyscript.cmd

leaves the new command window open (at the command prompt) when the script terminates. Similarly,

start /wait anyscript.cmd

does not return until the new command window is explicitly closed.

The work-around is straightforward: use

start /wait cmd /c anyscript.cmd

This will exit when the script does.

You may also want to consider whether you really want the new command window. Alternatives include

cmd /c anyscript.cmd

and

call anyscript.cmd

both of which will run the batch script in the existing command window.

Upvotes: 2

Related Questions