'Opening project in VSCode using batch file

Disclaimer: I read this and this before, but it doesn't work as I want.

Description: I decided to create set of batch files for convenient way to run different projects in VSCode from desktop in one click(double-click). I want close cmd terminal after running a batch file, but terminal remains. I tried:

start code "C:\Users\MyUserName\path\to\my\project\directory"

and

cmd /c start code "C:\Users\MyUserName\path\to\my\project\directory"

It quickly runs command, runs code and opens my project, then, it seems to me, closes terminal and runs a new one in desktop directory.



Solution 1:[1]

I found solution with help of DavidPostill. This works fine for me:

start "" cmd /b /c code "C:\Users\MyUserName\path\to\my\project\directory" && exit 0

UPDATE: There is a more simple way to run VSCode using command line interface:

cd path/to/my/project
code .

Solution 2:[2]

Try using: start cmd /C code . :0 It should be able to close the cmd terminal. At least that worked for me on my Windows 10.

Another version:
start cmd /C code "C:\Users\MyUserName\path\to\my\project\directory" :0

Solution 3:[3]

If anyone else comes across this in 2022, I found a solution that works great for me.

code "" "C:\path\to\folder\with\project" | exit

Also, below is my batch I made for a quick workspace that:

  1. asks for a folder name, this will also be used as the project name
  2. makes the vscode project
  3. makes 2 text files, one for things I had to look up, and another for answers to my question
  4. makes a png file called work.png that opens with paint for diagrams I might need for thinking through things
  5. Lastly (the part I love the most) it CLOSES the command window once everything is opened!

Hope this helps someone like me who doesn't know everything about batch files!

@echo off &setlocal
set /p "folder=Enter the folder name to be created: "
md "%folder%" ||(pause &goto :eof)
cd %folder%
echo. > Things_I_had_to_look_up.txt
echo. > Answers.txt
dotnet new console
xcopy /s "C:\xPaintFileTemplate" "C:\Users\TBD\Documents\Program Work\%folder%"
start mspaint.exe Work.png
code "" "C:\Users\TBD\Documents\Program Work\%folder%" | exit
Rem not needed but to be safe
exit

The C:\xPaintFileTemplate is a folder in my C drive that contains only a png file named Work.png. The png file is blank, and sized to what I want mspaint to open with. If there are more files in that folder, it will copy all of them, so be careful with xcopy!

The Rem is a comment, saying the exit command doesn't seem to be required but I added it in anyways as I believe it is good practice.

Solution 4:[4]

None of the above worked for me; at worst one of the left-over CMD's needed its close button clicking three times before it would go away.

I tried the URL method and this worked just as I wanted: VSCode opened, and the cmd window went away; my batch file ("VSCode on project.bat") contains just one line:

start vscode://file/C:/path/to/project

or:

start "" "vscode://file/C:/path/to/project"

Solution 5:[5]

One line code:

code C:\Users\MyUserName\path\to\my\project\directory pause

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Alvin-He
Solution 3
Solution 4 Dave the Sax
Solution 5 Joseph Freeman