3

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.

dgtlfx
  • 101
  • 1
  • 7
  • What is `code`? a program? – aschipfl Jul 02 '18 at 09:15
  • Start uses the first argument in quotes as the window title. So insert a dummy pair `start "" code "C:\Ussers\MyUserName\path\to\my\project\directory"` @aschipfl code is a batch in the path `C:\Program Files\Microsoft VS Code\bin\code.cmd` –  Jul 02 '18 at 09:35
  • @LotPings, I tried it, but result is the same, excepting the terminal's title. Now it is empty. – dgtlfx Jul 02 '18 at 09:48
  • `cmd /c start "" /d "C:\Users\MyUserName\path\to\my\project\directory" code` – DavidPostill Jul 02 '18 at 10:20
  • @DavidPostill thank you, but Code was opened with default empty project, and terminal remained opened in my project directory. – dgtlfx Jul 02 '18 at 10:29
  • Ok. Then try `start "" cmd /c code "C:\Users\MyUserName\path\to\my\project\directory"` – DavidPostill Jul 02 '18 at 10:34
  • @DavidPostill now it starts code in project, but empty cmd window remains without title. – dgtlfx Jul 02 '18 at 10:39
  • Then use `start "" cmd /b code "C:\Users\MyUserName\path\to\my\project\directory"` – DavidPostill Jul 02 '18 at 10:48
  • @DavidPostill this just open terminal at desktop directory, but I modified it a little bit, and this works excellent: `start "" cmd /b /c code "C:\Users\MyUserName\path\to\my\project\directory" && exit 0` Thank you, David! – dgtlfx Jul 02 '18 at 10:57

6 Answers6

5

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 .
dgtlfx
  • 101
  • 1
  • 7
  • Nothing happens if I use the `/b` switch. I don't think there is a `/b` switch if you check with `cmd /?` – andy Oct 08 '21 at 11:14
5

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.

1

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"
Dave the Sax
  • 328
  • 2
  • 12
  • This doesn't work if you try and open two VS Code instances in one batch file. A console is left hanging. For once instance it works. – andy Oct 08 '21 at 11:15
1

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

Alvin-He
  • 21
  • 1
  • 5
1

Based on Blake Daugherty's answer, I found the first pair of double quotes seems unnecessary:

code "D:\proj\directory-1" | exit

code "D:\proj\directory-2" | exit

exit
LittleSaya
  • 156
  • 2
  • 7
0

One line code:

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