通过双击或从cmd窗口检测bat文件是否正在运行 [英] Detect if bat file is running via double click or from cmd window
问题描述
我有一个bat文件,做一堆东西,并关闭cmd窗口,这是很好,当用户双击从资源管理器的bat文件。但是如果我从一个已经打开的cmd窗口运行bat文件,如在cmd> c:\myfile.bat然后我不想要该bat文件关闭cmd窗口(END),因为我需要做其他的事情。我需要bat dos命令代码,将执行像
I have a bat file that does a bunch of things and closes the cmd window which is fine when user double clicks the bat file from explorer. But if I run the bat file from a already open cmd window as in cmd>c:\myfile.bat then I do not want the bat file to close the cmd window (END) since I need to do other things. I need bat dos command code that will do something like
if (initiated_from_explorer) then
else
endif
这可能吗?感谢
推荐答案
%cmdcmdline%
提供用于启动的确切命令行当前Cmd.exe。
%cmdcmdline%
gives the exact command line used to start the current Cmd.exe.
- 从命令控制台启动时,此var为
%SystemRoot%\system32\cmd.exe
。 - 从explorer启动时,此var为
cmd / c{full_path_to_the_bat_file}
;
这意味着您也可以在您的bat文件中检查%0
变量,在这种情况下 始终是bat文件的完整路径,并且始终用双引号括起来。
- When launched from a command console, this var is
"%SystemRoot%\system32\cmd.exe"
. - When launched from explorer this var is
cmd /c ""{full_path_to_the_bat_file}" "
;
this implicates that you might also check the%0
variable in your bat file, for in this case it is always the full path to the bat file, and always enclosed in double quotes.
就我个人而言, c>%cmdcmdline%方法(不%O
),但是注意两个启动命令都可以在注册表中覆盖...
Personally, I would go for the %cmdcmdline%
approach (not %O
), but be aware that both start commands can be overridden in the registry…
这篇关于通过双击或从cmd窗口检测bat文件是否正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!