简体   繁体   English

使用批处理文件在 VSCode 中打开项目

[英]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).描述:我决定创建一组批处理文件,以便在 VSCode 中通过单击(双击)从桌面运行不同的项目。 I want close cmd terminal after running a batch file, but terminal remains.我想在运行批处理文件后关闭 cmd 终端,但终端仍然存在。 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.它快速运行命令,运行代码并打开我的项目,然后在我看来,关闭终端并在桌面目录中运行一个新终端。

I found solution with help of DavidPostill .我在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:更新:有一种使用命令行界面运行 VSCode 的更简单方法:

cd path/to/my/project
code .

Try using: start cmd /C code . :0尝试使用: start cmd /C code . :0 start cmd /C code . :0 It should be able to close the cmd terminal. start cmd /C code . :0它应该能够关闭 cmd 终端。 At least that worked for me on my Windows 10.至少这在我的 Windows 10 上对我有用。

Another version:另一个版本:
start cmd /C code "C:\\Users\\MyUserName\\path\\to\\my\\project\\directory" :0

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.在最坏的情况下,剩下的一个 CMD 需要点击 3 次关闭按钮才能消失。

I tried the URL method and this worked just as I wanted: VSCode opened, and the cmd window went away;我尝试了 URL 方法,结果如我所愿:VSCode 打开,cmd 窗口消失; my batch file ( "VSCode on project.bat" ) contains just one line:我的批处理文件( "VSCode on project.bat" )只包含一行:

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

or:或者:

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

If anyone else comes across this in 2022, I found a solution that works great for me.如果其他人在 2022 年遇到这种情况,我找到了一个对我来说非常有用的解决方案。

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制作 vscode 项目
  3. makes 2 text files, one for things I had to look up, and another for answers to my question制作 2 个文本文件,一个用于我必须查找的内容,另一个用于回答我的问题
  4. makes a png file called work.png that opens with paint for diagrams I might need for thinking through things制作一个名为 work.png 的 png 文件,该文件打开时会绘制我可能需要思考的图表

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\Interview Program Work\%folder%"
start mspaint.exe Work.png
code "" "C:\Users\TBD\Documents\Interview Program Work\%folder%" | exit
Rem not needed but to be safe
exit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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