Add an "Exit Command" to a shortcut


  1. Posts : 68
    Windows 10
       #1

    Add an "Exit Command" to a shortcut


    I'm not sure what to call this, but I'm curious if there is a way to tell a program to run an "Exit Command" when it's process closes. As an example, I can do this with a batch file. If I do the following, the command prompt will stop after it runs "program.exe" and then run the rest of the script when it closes:

    Code:
    cd "location of program"
    program.exe
    <DO MORE THINGS>
    The problem is that, if for some reason the command prompt is closed before "program.exe", it never gets to <DO MORE THINGS>. Is there a Windows shortcut command to instruct the program to do something when it closes? Something that looks like below where a bat file is run when the program ends?:

    Code:
    Target: C:\Program.exe -<EXIT COMMAND=run Exit.bat>
    Win Version: Windows 10, 21H2
      My Computer


  2. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #2

    Hello @SSShadii,

    Are you talking about something like & Exit or & Exit /b? . . .

    > EXIT
    > How to Close Batch Files

    I hope this helps.
      My Computer


  3. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #3

    SSShadii said:
    I'm not sure what to call this, but I'm curious if there is a way to tell a program to run an "Exit Command" when it's process closes. As an example, I can do this with a batch file. If I do the following, the command prompt will stop after it runs "program.exe" and then run the rest of the script when it closes:
    What you want does not exist directly but you might achieve the same result with the batch file you are using
    Code:
    cd "location of program"
    program.exe
    < DO MORE THINGS>
    and you could add that all into a consolidated shortcut Target field but it'd be difficult to see what you were typing / pasting in there & hence difficult to debug. I'd suggest putting the commands into a batch file and running a shortcut to that instead.

    You will also need to check how your particular program behaves.
    - It might or might not release the shortcut / batch file as soon as the command program.exe runs.
    - If it does then having a closing command would be more difficult.
    - I suggest using this amended form as a test and looking to see if it reaches the end as soon as program.exe starts to run or if it waits until it is closed.
    Code:
    cd "location of program"
    program.exe
    Pause The batch file has now progressed past the program.exe line
    If it passes straight through to that last line as soon as the program opens, then you'll need to try using the Start command,
    Code:
    cd "location of program"
    start "whatever" program.exe /WAIT
    Pause The batch file has now progressed past the program.exe line
    - See Start - SS64
    - But you will need to test this because Start does not always behave in the way you'd expect either. Sometimes it can pass straight through despite its /Wait switch. It depends on how program.exe itself behaves. It ignores the /Wait, just for example, in the case of Starting batch files or vbs scripts.


    All the best,
    Denis
      My Computer


  4. Posts : 2,799
    Linux Mint 20.1 Win10Prox64
       #4

    Example: Running bcdedit.exe command. This will require Admin privilege, so the shortcut must be set to run as Admin.
    cd /d %~dp0
    start /b %SYSTEMROOT%\system32\bcdedit.exe > %temp%\~foo.txt & exit

    The Command prompt working directory is: C:\Windows\system32 so when invoked, it cannot find the bat file, you must change
    to where your bat file resides. if program.exe is not in the same directory as the batch file then must also provide full path for it.
    - cd /d %~dp0 ===> change to directory where the bat file is.
    - start /b ==> will run the program without command prompt windows.
    - > %temp%\~foo.txt ===> output the result, remove if not needed
    - & to run 'exit' after the program.exe is finished whether it succeeds or not. else use: &&

    Note: double click on the shortcut will run silently, check the tempdir, should be a file ~foo.txt containing the result output.
      My Computer


  5. Posts : 1,203
    11 Home
       #5

    I can paste this in the Target field of a new shortcut:
    cmd /c start /b /wait "" "%ProgramFiles%\Windows Mail\wabmig.exe"&echo Done!&pause
    Then when I double-click, the echo Done!&pause part doesn't get executed before I hit Close.
      My Computers


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 07:43.
Find Us




Windows 10 Forums