首页 > 解决方案 > 是否可以将此多行批处理脚本转换为可工作的单行脚本?

问题描述

我想将我的多行批处理脚本变成一个衬里,这样我就可以将它用作文件资源管理器上下文菜单条目。我知道我可以链接注册表中的批处理文件来调用批处理脚本,它确实有效。

我正在尝试更加精通我的编码,所以我想知道是否有人可以看一下并给我一些指示。我一直在研究和测试以自己找到答案,但没有找到答案。如果我在注册表命令键中链接批处理脚本路径,我将包含两个脚本,它们都实现相同的目标并且都可以工作。

Script A更复杂,并根据运行期间遇到的情况为代码提供更多选项。Script B比较简单,最终还是达到了脚本的主要目的。

脚本 A 在自定义上下文菜单中从注册表中调用,该菜单在注册表项的命令键中运行这行代码。

"C:\Users\jholl\OneDrive\Documents\01_Scripts\01_REGISTRY_MAIN\Registry_Edits\File_Explorer\Context_Menu\Restart_Explorer\Restart_Explorer.bat" "%V"

"%V"我在 bat 文件末尾添加 的原因是将自定义命令行参数传递给批处理脚本,这样当我使用文件夹的 directory\background 访问上下文菜单并单击名为“Restart Explorer”的菜单条目时,它会捕获在 taskkill 关闭 explorer.exe 的所有实例后,当我在脚本末尾重新启动 explorer.exe 时,我可以使用“%~1”参数中的当前目录将文件资源管理器重新打开到同一文件夹。

这比通过文件资源管理器导航再次手动重新定位文件夹要方便得多。

@echo off
setlocal
prompt $g

call :restart_explorer "%~1"
goto:eof

:restart_explorer
tasklist /fi "imagename eq explorer.exe" | find /i /n "explorer.exe" >nul
if "%errorlevel%"=="0" (
start "" /wait taskkill /f /im explorer.exe &&^
start "" explorer &&^
start "" /max explorer "%~1" &&^
exit /b
) else (
goto failed
)

:failed
cls && echo.
echo taskkill was unable to find/close explorer.exe
echo it is most likely not running
timeout 5 >nul
exit /b

脚本 B 只是一个简化的代码。不再。与上述相同的目标,步骤更少。

@echo off
setlocal
prompt $g

call :restart_explorer "%~1"
goto:eof

:restart_explorer
tasklist /fi "imagename eq explorer.exe" | find /i /n "explorer.exe" >nul
if "%errorlevel%"=="0" (start "" /wait taskkill /f /im explorer.exe)
start "" explorer &&^
start "" /max explorer "%~1" &&^
exit /b

任何可以帮助我提振精神的想法都会受到极大的赞赏。我再次希望把它变成一个班轮。

标签: windowsperformancebatch-filescriptingregistry

解决方案


推荐阅读