习惯了使用绿色版软件,VSCode日常使用频率很高,记录一下如何使用windows批处理添加右键菜单以及注册文件关联。

新建任意文件,文件后缀改为cmd或者bat,内容如下:

chcp 65001
@echo off
setlocal

:: 获取VSCode的路径
set "vscodePath=%~dp0Code.exe"

:: 文件类型列表
set "fileTypes=php js html css scss yaml yml conf ini toml json jsp asp cpp bat cmd reg sh profile config bashrc md"

:: 显示菜单
:menu
cls
echo.
echo 1. 创建右键菜单
echo 2. 删除右键菜单
echo 3. 关联文件类型到VSCode
echo 4. 退出
echo.
set /p "choice=请输入你的选择(1-4): "

:: 根据用户的选择执行相应的操作
if "%choice%"=="1" goto create
if "%choice%"=="2" goto delete
if "%choice%"=="3" goto associate
if "%choice%"=="4" goto end
goto menu

:: 创建右键菜单
:create
reg add "HKCR\*\shell\Open with VSCode" /ve /d "Open with VSCode" /f
reg add "HKCR\*\shell\Open with VSCode" /v "Icon" /d "%vscodePath%" /f
reg add "HKCR\*\shell\Open with VSCode\command" /ve /d "\"%vscodePath%\" \"%%1\"" /f
reg add "HKCR\Directory\shell\Open with VSCode" /ve /d "Open with VSCode" /f
reg add "HKCR\Directory\shell\Open with VSCode" /v "Icon" /d "%vscodePath%" /f
reg add "HKCR\Directory\shell\Open with VSCode\command" /ve /d "\"%vscodePath%\" \"%%1\"" /f
echo.
echo 右键菜单已创建。
pause
goto menu

:: 删除右键菜单
:delete
reg delete "HKCR\*\shell\Open with VSCode" /f
reg delete "HKCR\Directory\shell\Open with VSCode" /f
echo.
echo 右键菜单已删除。
pause
goto menu

:: 关联文件类型到VSCode
:associate
for %%i in (%fileTypes%) do (
    reg add "HKCR\.%%i\OpenWithProgids" /v "VSCode.%%i" /f
    reg add "HKCR\VSCode.%%i\shell\open\command" /ve /d "\"%vscodePath%\" \"%%1\"" /f
)
echo.
echo 文件类型已关联到VSCode。
pause
goto menu

:: 退出
:end
endlocal

fileTypes的值改为需要关联的文件类型

脚本是AI写的。。