Nick Div
Nick Div

Reputation: 5628

How to run multiple commands in cmd using a script

I have three different commands that I want to execute my running one script,

I created a file called as myscript.bat

I want the following two commands to run in sequence when I run the script:

cd C:\Users\johnDoe\Idea\View\Proxy

node proxy.js

PS:And after that I want the cmd to be left open, not close automatically

Upvotes: 0

Views: 2881

Answers (2)

mklement0
mklement0

Reputation: 440112

@reirab's answer contains the crucial pointer: use cmd /k to create a console window that stays open.

If you don't want to use cmd /k explicitly - say, because you want to open the batch file from Explorer, you have 2 options:

  • [Suboptimal] Add a cmd /k statement as the last statement to your batch file.
  • [Better] Write a wrapper batch file that invokes the target batch file with cmd /k.

For instance, if your batch file is startProxy.bat, create another batch file in the same folder, name it startProxyWrapper.bat, and assign the following content:

@cmd /k "%~dp0startProxy.bat"

When you then open startProxyWrapper.bat from Explorer, a persistent console window (one that stays open) will open and execute startProxy.bat.

Upvotes: 1

reirab
reirab

Reputation: 1599

Leave the bat file as it is, but run it with cmd /k myscript.bat. Also, if there's a chance of the command window opening by default on another drive, you might want to add the line C: to the beginning of the script to change volumes. cd changes folders within a given volume, but it doesn't actually change volumes.

Alternatively, if you just want the window to stay open so you can read the output, but you don't actually want to run any additional commands in it after your commands finish, you can just add the line pause at the end of the script.

Upvotes: 1

Related Questions