Home > database >  Why change of sequence in cmd command gives different result?
Why change of sequence in cmd command gives different result?

Time:02-12

I'm new in using cmd and batch script. Here is what I was trying to do with a .bat file.

  1. go to a folder
  2. open that folder in file explorer
  3. open that folder in vscode
  4. start Firefox browser

Sequence doesn't matter here. Doing these tasks in any sequence is fine.

Here are the commands that I initially wrote

cd/
E:
cd folder1/folder2/folder3
code .
start .
start firefox

But with these commands the result I got is

  1. Only vscode started with the folder I wanted to open with vscode
  2. File explorer and Firefox was not starting
  3. cmd.exe continued to run. But it seems like it only executed upto code . command
  4. When I closed vscode, cmd.exe would be closed with it too

Then I changed the sequence of the commands as below

cd/
E:
cd folder1/folder2/folder3
start .
start firefox
code .

This time everything worked as expected. I've looked up documentation for start command but didn't find anything(or maybe I didn't understand) about start . command.
Can anyone explain me why the result changed when I changed the sequence of the commands?

CodePudding user response:

As already mentioned in the comments, code is really a batch file, code.cmd.

If your VSCode installer added a location to your %PATH%, take a look there and you should see code.cmd in that \bin directory.

When you run a batch file from another, if you want control to pass back to the initial script upon completion, you need to run it using the call command.

@CD /D "E:\folder1\folder2\folder3" 2>NUL || Exit /B
@Call "%ProgramFiles%\Microsoft VS Code\bin\code.cmd" .
@Start "" "%SystemRoot%\explorer.exe" .
@Start "" "%ProgramFiles%\Mozilla Firefox\firefox.exe"

If you do not want robust code, and wish to assume file extensions and environment settings, the above could be simplified to this:

@CD /D E:\folder1\folder2\folder3
@Call code .
@Start explorer .
@Start firefox
  • Related