windows - 是否可以将此多行批处理脚本转换为可工作的单行脚本?
问题描述
我想将我的多行批处理脚本变成一个衬里,这样我就可以将它用作文件资源管理器上下文菜单条目。我知道我可以链接注册表中的批处理文件来调用批处理脚本,它确实有效。
我正在尝试更加精通我的编码,所以我想知道是否有人可以看一下并给我一些指示。我一直在研究和测试以自己找到答案,但没有找到答案。如果我在注册表命令键中链接批处理脚本路径,我将包含两个脚本,它们都实现相同的目标并且都可以工作。
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
任何可以帮助我提振精神的想法都会受到极大的赞赏。我再次希望把它变成一个班轮。
解决方案
推荐阅读
- csv - Perl 文本::CSV_XS | getline() 正在合并行
- tensorflow - 如何让 Tensorflow Profiler 在 Tensorflow 2.5 中使用“tensorflow-macos”和“tensorflow-metal”工作
- kubernetes - Istio - 在 Prometheus 中看不到默认指标
- r - 编织到pdf时是否可以添加*完全*整页图像?
- typescript - 我怎样才能完成下面的任务?我必须从什么开始?
- asp.net-core - 在 dotnet core/razor 页面中存储/检索静态数据的最有效方法
- node.js - 如何在 Docker 容器上运行 Angular 项目?
- webpack - 无法在 Electron-Forge/Webpack 中加载内联字体
- javascript - div三角形不显示
- c# - 单元测试 Polly - 检查是否在超时/错误时触发重试策略