确定批处理脚本是否已从命令行 (cmd) 启动/执行 - 或 - 暂停还是不暂停?
我喜欢在我的 cmd.exe 脚本中使用典型的“usage:”行 - 如果缺少参数,系统会简单提醒用户如何使用该脚本。
问题是我无法安全地预测潜在用户是否会使用 GUI 还是 CLI。如果使用 GUI 的人在资源管理器窗口中双击此脚本,他们将没有机会读取任何内容,除非我暂停
窗口。如果他们使用 CLI,暂停
会打扰他们。
所以我正在寻找一种方法来检测它。
@echo off
if _%1_==__ (
echo usage: %nx0: filename
rem now pause or not to pause?
)
这方面有好的解决办法吗?
I like to have a typical "usage:" line in my cmd.exe
scripts — if a parameter is missing, user is given simple reminder of how the script is to be used.
The problem is that I can't safely predict whether potential user would use GUI or CLI. If somebody using GUI double-clicks this script in Explorer window, they won't have chance to read anything, unless I pause
the window. If they use CLI, pause
will bother them.
So I'm looking for a way to detect it.
@echo off
if _%1_==__ (
echo usage: %nx0: filename
rem now pause or not to pause?
)
Is there a nice solution on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以检查
%CMDCMDLINE%
变量的值。它包含用于启动 cmd.exe 的命令。我准备了一个测试 .bat 文件:
当从打开的
cmd.exe
窗口内部运行时,脚本会打印"C:\Windows\system32\cmd.exe"
。通过双击运行时,它会打印
cmd /c ""C:\Users\mbu\Desktop\test.bat" "
因此,要检查脚本是否通过双击启动,您需要检查
%cmdcmdline%
是否包含脚本的路径。最终的解决方案如下所示:编辑:针对评论中讨论的问题更改实施了修复;编辑示例来演示它们
You can check the value of
%CMDCMDLINE%
variable. It contains the command that was used to launchcmd.exe
.I prepared a test .bat file:
When run from inside of open
cmd.exe
window, the script prints"C:\Windows\system32\cmd.exe"
.When run by double-clicking, it prints
cmd /c ""C:\Users\mbu\Desktop\test.bat" "
So to check if your script was launched by double-clicking you need to check if
%cmdcmdline%
contains the path to your script. The final solution would look like this:Edit: implemented fixes for issues changes discussed in comments; edited example to demonstrate them
在这里,我写了一些东西...
Usage.bat
它并不完美,但它有效! :D-
乔德夫
Here, I wrote something...
Usage.bat
It's not perfect, but it works! :D
-joedf
这仅使用内部命令。如此有效....
或
或
This is only using the internal command. so effectively....
or
or
开始批量检查 %cmdcmdline% 中的 %WINDIR%,如下所示:
Start your batch checking for %WINDIR% in %cmdcmdline% like this:
请使用 findstr
或
这始终有效...
Please use findstr
or
This is always worked...
对于内部命令
或
您可以比较不同的东西,但这仅在 EnableDelayedExpansion 中有效。而且我不认为这总是有效,因为 Windows 版本等等......
for internal command
or
You can compare the different thing, but this is only worked within EnableDelayedExpansion. and I don't think that this will be always worked, cause windows version, etc...
类似的做法...
Similar approach...
测试是在Windows 10中完成的。使用%windir%,它有点危险或不明确。所以 %comspec% 是超级安全的。
Test is done in Windows 10. Using %windir%, it is a little dangerous or ambiguous. So %comspec% is super safe.
这仅对带有复制和行的逐行输入有效。粘贴批处理程序。当我检测到我将剪贴板复制到 %temp% 文件夹中的 cmd 文件中并要求用户从那里执行该文件时:
This is valid only for a line by line entry with copy & paste of a batch program. When I detect that I copy the clipboard into a cmd file in the %temp% folder and ask the user to execute the file from there: