Detect if bat file is running via double click or from cmd window

前端 未结 12 1534
醉话见心
醉话见心 2020-12-29 02:54

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 al

相关标签:
12条回答
  • 2020-12-29 03:10

    You can add a command line parameter when running from a CMD window that won't exist when the file is double-clicked. If there is no parameter, close the window. If there is, don't close it. You can test the parameter using %1

    0 讨论(0)
  • 2020-12-29 03:14

    You also can check for SESSIONNAME environment variable.

    As you see here that variable typically isn't set in Explorer window. When invoking from cmd it SESSIONNAME is set to Console. I can confirm this for Windows 10.

    Unfortunately behaviour seems to be changeable: https://support.microsoft.com/de-de/help/2509192/clientname-and-sessionname-enviroment-variable-may-be-missing

    0 讨论(0)
  • 2020-12-29 03:15

    mousio's solution is nice; however, I personally did not manage to make it work in an "IF" statement because of the double quotes in the value of %cmdcmdline% (with or without double quotes around %cmdcmdline%).

    In constrast, the solution using %0 works fine. I used the following block statement and it works like a charm:

    IF %0 == "%~0"  pause
    

    The following solution, which expands %~0 to a fully qualified path, might also work if the previous does not (cf. Alex Essilfie's comment):

    IF %0 EQU "%~dpnx0" PAUSE
    

    However, note that this solution with %~dpnx0 fails when

    1. the .bat file is located somewhere in the %USERPROFILE% directory, and
    2. your %USERNAME% contains one or more uppercase characters

    because... wait for it... the d in %~dpnx0 forces your %USERPROFILE% username to lowercase, while plain %0 does not. So they're never equal if your username contains an uppercase character. ¯\_(ツ)_/¯

    0 讨论(0)
  • 2020-12-29 03:17

    A consolidated answer, derived from much of the information found on this page:

    :pauseIfDoubleClicked
    setlocal enabledelayedexpansion
    set testl=%cmdcmdline:"=%
    set testr=!testl:%~nx0=!
    if not "%testl%" == "%testr%" pause
    
    1. The variable "testl" gets the full line of the cmd processor call (as per mousio), stripping out all of the pesky double quotes.
    2. The variable "testr" takes "testl" and further strips outs the name of the current batch file name if present (which it will be if the batch file was invoked with a double-click).
    3. The if statement sees if "testl" and "testr" are different. If yes, batch was double-clicked, so pause; if no, batch was typed in on command line, go on.

    Naturally, if you want to do something else if you detect a double-click, you can change the pause.

    Thanks everyone.

    0 讨论(0)
  • 2020-12-29 03:17

    Less code, more robust:

    Build upon the other answers, I find the most robust approach to:

    1. Replace quotes with x to enable text comparison without breaking the IF statement.
    2. Recreate the expected cmdcmdline exactly (well, with '"' replaced by x).
    3. Test for case-insensitive equality.

    The result is:

    set "dclickcmdx=%systemroot%\system32\cmd.exe /c xx%~0x x"
    set "actualcmdx=%cmdcmdline:"=x%"
    
    set isdoubleclicked=0
    if /I "%dclickcmdx%" EQU "%actualcmdx%" (
        set isdoubleclicked=1
    )
    

    This adds more robustness against general cmd /c calls, since Explorer adds an awkward extra space before the last quote/x (in our favor). If cmdcmdline isn't found, it correctly renders isdoubleclicked=0.

    0 讨论(0)
  • 2020-12-29 03:18

    Like @anishsane I too wanted a pause statement if launched from explorer, but not when launched from a command window.

    Here's what worked for me, based upon @mousio's answer above:

    @SET cmdcmdline|FINDSTR /b "cmdcmdline="|FINDSTR /i pushd >nul
    @IF ERRORLEVEL 1 (
        @echo.
        @echo Press ENTER when done
        @pause > nul
    )
    

    (Nothing original here, just providing a working example)

    0 讨论(0)
提交回复
热议问题