简体   繁体   中英

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窗口

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.

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 

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.

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 .

快捷方式属性窗口

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM