@echo off
setlocal
rem Process 1st argument passed.
if "%~1" == "/?" goto :help
if "%~1" == "/query" goto :query
if "%~1" == "/register" goto :register
if "%~1" == "/unregister" goto :unregister
if "%~1" == "" exit /b 1
goto :search
exit /b 0
:help
echo Open chrome search page with filename as the item to search.
echo:
echo Syntax: "%~nx0" keyword
echo:
echo Arguments:
echo /?
echo This help message.
echo /query
echo View a query of registered entries.
echo /register
echo Register context menu entries.
echo /unregister
echo Unregister context menu entries.
exit /b 0
:query
reg query "HKLM\software\Classes\*\shell\Subtitle Search" /s
exit /b 0
:register
echo Register subtitle search
reg add "HKLM\software\Classes\*\shell\Subtitle Search" /d "Subtitle Search" /f
reg add "HKLM\software\Classes\*\shell\Subtitle Search\Command" /d "\"%~f0\" \"%%1\"" /f
if errorlevel 1 net session >nul 2>&1 || echo Require admin to register.
exit /b 0
:search
rem Filename as item to search. If want extension also, use nx modifiers instead of just n.
set "search=%~n1"
rem URL used as the search engine.
:: set "url=https://www.google.com/search?q="
set "url=https://www.startpage.com/do/search/?q="
:: set "url=http://subscene.com/subtitles/title?q="
rem Use start so that the window does not remain open. CMD not being called so surrounding double quotes not stripped.
start "" "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "%url%%search%"
rem View CMD /?. Surrounding double quotes and more than 1 pair may have the outer pair stripped. Add another escaped pair to be stripped.
:: cmd /s /c ^""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "%url%%search%"^"
rem Uncomment command to check commandline of cmd.exe.
:: wmic process where caption="cmd.exe" get commandline & pause
exit /b 0
:unregister
echo Unregister subtitle search
reg delete "HKLM\software\Classes\*\shell\Subtitle Search" /f
if errorlevel 1 net session >nul 2>&1 || echo Require admin to unregister.
exit /b 0
代码中的问题:
我已将脚本合二为一,您可以通过以下方式注册:
search /register
预期输出:
Register subtitle search
The operation completed successfully.
The operation completed successfully.
确保将命令提示符用作管理员,因为注册到HKLM 分支可能需要它。脚本所在的位置是添加到注册表的路径。所以不要在任何地方注册并稍后将其移动到正确的路径。
通过以下方式查询注册表项:
search /query
预期输出:
HKEY_LOCAL_MACHINE\software\Classes\*\shell\Subtitle Search
(Default) REG_SZ Subtitle Search
HKEY_LOCAL_MACHINE\software\Classes\*\shell\Subtitle Search\Command
(Default) REG_SZ "C:\My Projects\My Code\search.cmd" "%1"
查看帮助信息:
search /?
如果取消注释 :search 标签中的 wmic 命令,则在打开的控制台中进行搜索的输出为:
CommandLine
cmd /c ""C:\My Projects\My Code\search.cmd" "C:\My Projects\My Code\Alice In Wonderland.mp4""
Press any key to continue . . .
默认情况下,此命令应作为注释禁用或删除,仅用于检查命令行。
如果 startpage.com 用于搜索引擎,Chrome 会在此 URL 处打开:
https://www.startpage.com/do/search/?q=Alice%20In%20Wonderland
如果提到的这两个参数都没有传递,并且第一个参数是什么,那么搜索是用 Chrome 完成的。没有参数只会结束脚本,因为它对处理任何内容都没用。
我无法访问subscene.com 站点,因此我添加了一些其他 URL 用于测试。
cmd 命令被禁用,start 的使用已添加,因此控制台窗口关闭并打开 Chrome。我在脚本中留下了cmd,所以你可以看到它可以工作。
注销:
search /unregister
预期输出:
Unregister subtitle search
The operation completed successfully.
确保将命令提示符用作管理员,因为从 HKLM 分支注销可能需要它。脚本的位置无关紧要,因为它会删除整个注册表项。