检测一个批处理文件如何执行 [英] Detecting how a batch file was executed

查看:79
本文介绍了检测一个批处理文件如何执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设Windows中,有一个方法可以让我从一个批处理文件内检测,如果它是从一个打开命令提示符或双击启动?我想暂停添加到所述批处理过程的结束时,如果且仅当它被双点击,使得窗口不只是它可能已产生的任何有用的输出消失沿

Assuming Windows, is there a way I can detect from within a batch file if it was launched from an open command prompt or by double-clicking? I'd like to add a pause to the end of the batch process if and only if it was double clicked, so that the window doesn't just disappear along with any useful output it may have produced.

任何聪明的方式做到这一点?我正在寻找解决方案,我可以依靠已配置或多或少使用默认设置的机器上工作。

Any clever ways to do this? I'm looking for solutions I could rely on to work on a machine that was configured more or less with default settings.

推荐答案

我只是跑了一个快速测试,发现以下,这可以帮助你:

I just ran a quick test and noticed the following, which may help you:


  • 当从一个打开的命令提示符下运行,在%0变量没有路径周围的双引号。如果脚本驻留在当前目录,路径甚至不给,只是批处理文件名。

  • 当从资源管理器中运行时,%0变量总是用双引号并包含完整路径的批处理文件。

这个脚本不会暂停如果从命令控制台运行,但在资源管理器中,如果将双击:

This script will not pause if run from the command console, but will if double-clicked in Explorer:

@echo off
setlocal enableextensions

set SCRIPT=%0
set DQUOTE="

@echo do something...

@echo %SCRIPT:~0,1% | findstr /l %DQUOTE% > NUL
if %ERRORLEVEL% EQU 0 set PAUSE_ON_CLOSE=1

:EXIT
if defined PAUSE_ON_CLOSE pause

编辑:
从资源管理器中运行,我无法解释时,也有一些怪异的行为。原本,而不是

There was also some weird behavior when running from Explorer that I can't explain. Originally, rather than

@echo %SCRIPT:~0,1% | findstr /l %DQUOTE% > NUL
if %ERRORLEVEL% EQU 0 set PAUSE_ON_CLOSE=1

我试着使用只是一个如果

if %SCRIPT:0,1% == ^" set PAUSE_ON_CLOSE=1

这从一个打开的命令提示符下运行时的工作,但是从Explorer中运行时,它会抱怨如果说法是不正确的。

这篇关于检测一个批处理文件如何执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆