@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
(PUSHD "%~DP0") && (REG QUERY "HKU\S-1-5-19" >NUL 2>&1) || (
powershell -Command "Start-Process '%~sdpnx0' -Verb RunAs" && EXIT)

:MENU
CLS
ECHO.&ECHO 1、添加系统右键 EmEditor 查找项
ECHO.&ECHO 2、移除系统右键 EmEditor 查找项
ECHO.&ECHO.

IF EXIST "%SystemRoot%\System32\choice.exe" (
    CHOICE /C 12 /N /M "请输入数字 (1,2): "
    SET choice=!ERRORLEVEL!
) ELSE (
    SET /P choice="请输入数字 (1,2): "
    SET choice=!choice:~0,1!
)

IF "!choice!"=="1" GOTO AddMenu
IF "!choice!"=="2" GOTO RemoveMenu
GOTO MENU

:AddMenu
:: 添加空白处右键菜单
reg add "HKCR\Directory\Background\shell\EmEditor" /t REG_SZ /d "用 EmEditor 在文件中查找" /f >NUL 2>NUL
reg add "HKCR\Directory\Background\shell\EmEditor" /v "Icon" /t REG_SZ /d "%~dp0emedres.dll,-128" /f >NUL 2>NUL
reg add "HKCR\Directory\Background\shell\EmEditor\command" /t REG_SZ /d "\"%~dp0emeditor.exe\" /fd \"%%V\"" /f >NUL 2>NUL

reg add "HKCU\SOFTWARE\Classes\Directory\Background\shell\EmEditor" /t REG_SZ /d "用 EmEditor 在文件中查找" /f >NUL 2>NUL
reg add "HKCU\SOFTWARE\Classes\Directory\Background\shell\EmEditor" /v "Icon" /t REG_SZ /d "%~dp0emedres.dll,-128" /f >NUL 2>NUL
reg add "HKCU\SOFTWARE\Classes\Directory\Background\shell\EmEditor\command" /t REG_SZ /d "\"%~dp0emeditor.exe\" /fd \"%%V\"" /f >NUL 2>NUL

:: 添加目录右键菜单
reg add "HKCR\Directory\shell\EmEditor" /t REG_SZ /d "用 EmEditor 在文件中查找" /f >NUL 2>NUL
reg add "HKCR\Directory\shell\EmEditor" /v "Icon" /t REG_SZ /d "%~dp0emedres.dll,-128" /f >NUL 2>NUL
reg add "HKCR\Directory\shell\EmEditor\command" /t REG_SZ /d "\"%~dp0emeditor.exe\" /fd \"%%1\"" /f >NUL 2>NUL

reg add "HKCU\SOFTWARE\Classes\Directory\shell\EmEditor" /t REG_SZ /d "用 EmEditor 在文件中查找" /f >NUL 2>NUL
reg add "HKCU\SOFTWARE\Classes\Directory\shell\EmEditor" /v "Icon" /t REG_SZ /d "%~dp0emedres.dll,-128" /f >NUL 2>NUL
reg add "HKCU\SOFTWARE\Classes\Directory\shell\EmEditor\command" /t REG_SZ /d "\"%~dp0emeditor.exe\" /fd \"%%1\"" /f >NUL 2>NUL

:: 添加盘符右键菜单
reg add "HKCR\Drive\shell\EmEditor" /t REG_SZ /d "用 EmEditor 在文件中查找" /f >NUL 2>NUL
reg add "HKCR\Drive\shell\EmEditor" /v "Icon" /t REG_SZ /d "%~dp0emedres.dll,-128" /f >NUL 2>NUL
reg add "HKCR\Drive\shell\EmEditor\command" /t REG_SZ /d "\"%~dp0emeditor.exe\" /fd \"%%V\"" /f >NUL 2>NUL

reg add "HKCU\SOFTWARE\Classes\Drive\shell\EmEditor" /t REG_SZ /d "用 EmEditor 在文件中查找" /f >NUL 2>NUL
reg add "HKCU\SOFTWARE\Classes\Drive\shell\EmEditor" /v "Icon" /t REG_SZ /d "%~dp0emedres.dll,-128" /f >NUL 2>NUL
reg add "HKCU\SOFTWARE\Classes\Drive\shell\EmEditor\command" /t REG_SZ /d "\"%~dp0emeditor.exe\" /fd \"%%V\"" /f >NUL 2>NUL

:: 添加文件右键菜单
reg add "HKCR\*\shell\EmEditor" /t REG_SZ /d "用 EmEditor 打开" /f >NUL 2>NUL
reg add "HKCR\*\shell\EmEditor" /v "Icon" /t REG_SZ /d "%~dp0emedres.dll,-128" /f >NUL 2>NUL
reg add "HKCR\*\shell\EmEditor\command" /t REG_SZ /d "\"%~dp0emeditor.exe\" \"%%1\"" /f >NUL 2>NUL

reg add "HKCU\SOFTWARE\Classes\*\shell\EmEditor" /t REG_SZ /d "用 EmEditor 打开" /f >NUL 2>NUL
reg add "HKCU\SOFTWARE\Classes\*\shell\EmEditor" /v "Icon" /t REG_SZ /d "%~dp0emedres.dll,-128" /f >NUL 2>NUL
reg add "HKCU\SOFTWARE\Classes\*\shell\EmEditor\command" /t REG_SZ /d "\"%~dp0emeditor.exe\" \"%%1\"" /f >NUL 2>NUL


ECHO.&ECHO 添加完成, 任意键返回!
PAUSE>NUL&CLS&GOTO MENU

:RemoveMenu
@REM 删除空白处右键菜单
reg delete "HKCR\Directory\Background\shell\EmEditor" /f >NUL 2>NUL
reg delete "HKCU\SOFTWARE\Classes\Directory\Background\shell\EmEditor" /f >NUL 2>NUL
 
@REM 删除目录右键菜单
reg delete "HKCR\Directory\shell\EmEditor" /f >NUL 2>NUL
reg delete "HKCU\SOFTWARE\Classes\Directory\shell\EmEditor" /f >NUL 2>NUL
 
@REM 删除盘符右键菜单
reg delete "HKCR\Drive\shell\EmEditor" /f >NUL 2>NUL
reg delete "HKCU\SOFTWARE\Classes\Drive\shell\EmEditor" /f >NUL 2>NUL
 
@REM 删除文件夹右键菜单
reg delete "HKCR\*\shell\EmEditor" /f >NUL 2>NUL
reg delete "HKCU\SOFTWARE\Classes\*\shell\EmEditor" /f >NUL 2>NUL

ECHO.&ECHO 删除完成, 任意键返回!
PAUSE>NUL&CLS&GOTO MENU