Tadas Petra
Tadas Petra

Reputation: 428

When opening vscode using batch file, cmd opens and doesn't close

When trying to open vscode folder using a batch file, Visual Studio opens up with that folder, but also a cmd window pops up and does not go away if you use exit command.

@echo off
start code "C:\GitHub\TestApp\testapp"
exit 

VSCode opens up correctly, but also this window opens

cmd window

Upvotes: 6

Views: 5591

Answers (5)

ProblemSolver05
ProblemSolver05

Reputation: 21

The only way I was able to overcome this is by putting taskkill /F /IM "cmd.exe" /T at the end of code.cmd file. It will solve the problem at the cost of killing all other cmd windows if there are other ones open.

Upvotes: 0

Simon
Simon

Reputation: 751

Using VSCode 1.52.1, the only way I could start it without having a cmd window open after exiting the batch script is:

explorer.exe "%userprofile%\AppData\Local\Programs\Microsoft VS Code\Code.exe"

Note: it does not involve opening a specific local directory to work with. But maybe you can find a solution such as saving the folder as a workspace or using Ctrl+R to open recent folders. Plus, if you work only within that directory / workspace, or use it right before closing VSCode, it will be opened automatically at the next launch.

Upvotes: 6

If all described solutions did not work for you, try making an ordinary Windows shortcut to "C:\Users\username\AppData\Local\Programs\Microsoft VS Code\Code.exe" C:\path-to-project-folder-or-file.

Shortcut properties window

Then call this shortcut in your .bat or .cmd script like that (assuming shortcut name is shortcut):

@echo off
start C:\path-to-shortcut-file\shortcut

Upvotes: 0

sst
sst

Reputation: 1463

That's because you are actually invoking the batch file code.cmd which is located at [VSCodePath]\bin\code.cmd. The code.cmd file in turn invokes the actual VSCode executable code.exe

When invoking a batch file (.BAT or .CMD) using the start command, a new instance of CMD process will be created to handle the execution of the batch file, But it invokes the CMD process with the /K switch rather than /C

For example start code.cmd executes cmd /k code.cmd

It is the /K switch that causes the new cmd to remain open after finishing the execution of the batch file.

To resolve, instead of supplying the batch file directly the to the start command, execute it by an explicit CMD invokation:

@echo off
start cmd /C code "C:\GitHub\TestApp\testapp"
exit 

Upvotes: 2

ifconfig
ifconfig

Reputation: 6832

That CMD window is associated with the VSCode instance that you just opened. Attempting to close it will terminate the application you started. (in this case, VSCode)

The start xxx xxx... command opens up a new cmd terminal to perform its action. Even though a new prompt appears, which can be used as a normal terminal itself, the VSCode process is inexorably linked to it as the parent process.

If your goal is to not launch a separate cmd window, then run:

start code /b "C:\GitHub\TestApp\testapp"

which just runs the command in the same window. The VSCode window is still inexorably bound to the current cmd window and will close if the cmd window disappears, but at least another cmd window isn't launched.

Windows doesn't have the capability to launch a program in the background from the terminal.

Upvotes: 1

Related Questions