CMD Script: How to close the CMD

59,994

Solution 1

Use the start command:

start "title" "%ProgramFiles%\Internet Explorer\iexplore.exe" http://www.example.com

Solution 2

you need this on the end

&& exit

For example

"%ProgramFiles%\Internet Explorer\iexplore.exe" http://google.co.uk && exit 

Solution 3

@echo off
start "" "%ProgramFiles%\Internet Explorer\iexplore.exe" "http://www.example.com"
exit /b

But you really should not force IE, but use the default browser:

@echo off
start http://www.example.com
exit /b

exit /b does not work on win9x IIRC, so if you need to support every version of windows and close the terminal window if the user double clicks your batch file, go with:

@echo off
start http://www.example.com
cls

Solution 4

You can also launch your program with the /c switch, which terminates the cmd once its finished executing

for example

cmd /c "%ProgramFiles%\InternetExplorer\iexplore.exe" http://localhost/test.html

Solution 5

You have to add 'start' in front of every program you launch, elsewhere your script is going to wait until it's finished.

Share:
59,994
Abs
Author by

Abs

Updated on June 14, 2021

Comments

  • Abs
    Abs almost 3 years

    I have created a small command that will let me launch Internet Explorer. However, I wish to close the small command prompt that shows up when I launch IE. How can I do this? This is my current code:

    "%ProgramFiles%\Internet
    Explorer\iexplore.exe"
    http://localhost/test.html
    PAUSE
    

    I am guessing if I take out the Pause. It will close the CMD box upon closing IE??

    Also is there another command that I can use to simply create a command that will let me add something to the Menu with a small icon, which in turn runs the above. Is this complicated? Any tutorials I can use?

    Thanks all

  • Abs
    Abs over 14 years
    Strange I have placed EXIT at the end but it only closes after I close IE. I was hoping to close it when it spawns off the IE process, possible?
  • Abs
    Abs over 14 years
    Just tried that same thing happens. I am on Vista by the way if that helps! I thought this was going to be a piece of cake but I haven't had my cake yet! I appreciate any more help.
  • Abs
    Abs over 14 years
    If I put an exit at the top, it obviously closes the CMD!
  • Abs
    Abs over 14 years
    This just spawned another CMD prompt!
  • svens
    svens over 14 years
    well just put it in front of your IE, that is what I wanted to say with programm
  • Abs
    Abs over 14 years
    No problem. I managed to get it working with the start title thing as suggested. +1 for continued help. Out of interest what OS are you running for the above to work. I am worried my current solution may not work on most Windows versions!
  • Russ Cam
    Russ Cam over 14 years
    Windows XP. But this should work in the CMD shell in Vista and 7 and right back to 3.1, I believe.
  • Dirk Vollmar
    Dirk Vollmar over 14 years
    I also couldn't get this working. On my XP machine the command window stays open until I close the browser. Maybe there is some misunderstanding in how the OP wants to call the script?
  • Abs
    Abs over 14 years
    I see. Thanks, I will try it on a different machine as I thought that would work.
  • Russ Cam
    Russ Cam over 14 years
    Aha - I don't think it's working because you've got it in a CMD/BAT file but I thought you were entering it into the CMD shell. Apologies, my mistake. This is not a bad resource to begin with - technet.microsoft.com/en-us/library/cc723564.aspx
  • Russ Cam
    Russ Cam over 14 years
    Interestingly, this appears to launch the default browser, as it launches FireFox for me :)
  • Anders
    Anders over 14 years
    why "title"?? use start "" "foo.exe"
  • Dirk Vollmar
    Dirk Vollmar over 14 years
    It is for no reason, just to make the sample more explicit. I thought people might wonder more about an empty string as the first parameter. But it seems I've been wrong ;-)
  • Dirk Vollmar
    Dirk Vollmar over 14 years
    @Russ: Did you omit the first parameter that is specifying the title? If you do start.exe will use the executable path as the title and then start the URL which gets opened by the default browser.
  • Dirk Vollmar
    Dirk Vollmar over 14 years
    "But you really should not force IE, but use the default browser": Maybe the requirement is to force the use of IE for a certain URL? I guess the OP will know better ;-)
  • Dirk Vollmar
    Dirk Vollmar over 14 years
    Ah, I see now. This is for calling interactively.
  • Bernardo Dal Corno
    Bernardo Dal Corno over 5 years
    another trick is to add "start explorer.exe " and the program, which is equivalent to launching the app within windows