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

5

6

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

Holston answered 22/6, 2019 at 0:43 Comment(1)
Have you tried without start?Faenza
I
1

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.

Icao answered 22/6, 2019 at 5:42 Comment(1)
But i am able to close the cmd window and use the visual studio code instance stillHolston
M
6

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.

Masculine answered 18/12, 2020 at 12:8 Comment(2)
This is great to have VS Code reopen the previous session.Foin
After running this command, you can use call code to open a new empty window, call code folder_path to open a folder in a new window, and call code path_to_file to open a file in the current window. The terminal does close at the end if VSCode is opened with explorer.exe firstAfflatus
H
2

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 
Hatti answered 23/6, 2019 at 8:8 Comment(4)
This method still makes the window pop up, however it pops up as a blank terminal with no directory. And closes once you close visual studio code it closes as wellHolston
@TadasPetraitis, The reason that the terminal window remains open while vs code is running is that you are probably running code.exe directly. In that case vs code attaches itself to the parent console, causes the console to remain open until it is closed. To resolve, try to to invoke code.cmd explicitly: start cmd /C code.cmd ..... however it causes it a terminal window to quickly popup and disappear. If don't like the popup flash add the /b switch to the start command parameters: start /b cmd /c code.cmd .....Hatti
Also you should be able to abandon the start command altogether and call the code.cmd file directly inside your own batch file: call code.cmd "C:\GitHub\TestApp\testapp"Hatti
@Hatti seems like this behaves the same as call code ... and the caller remains openNarcho
I
1

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.

Icao answered 22/6, 2019 at 5:42 Comment(1)
But i am able to close the cmd window and use the visual studio code instance stillHolston
B
0

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
Bronwyn answered 20/11, 2020 at 13:27 Comment(0)
W
0

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.

Wooded answered 15/9 at 21:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.