Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replicate command prompt with batch files

I am attempting to replicate the command prompt using batch files. My current code is reproduced below.

@echo off
title Command Prompt
:LOOP
set /p COMMAND="%CD%>"
title Command Prompt - %COMMAND%
call %COMMAND%
echo.
title Command Prompt
goto LOOP

However, I have one issue. If I'm calling a program (such as gpedit.msc), the batch file waits until the program returns before continuing, when a normal command prompt returns without waiting for the program to return.

How do I test if a command is a program, so I can use the start command?

like image 693
kevinji Avatar asked Nov 03 '22 12:11

kevinji


1 Answers

You can search the executable for user32.dll.

@echo off
title Command Prompt
:LOOP
    set /p COMMAND="%CD%>"
    title Command Prompt - %COMMAND%

    call :is_gui %COMMAND%
    if errorlevel 1 (
        call %COMMAND%
    ) else (
        start "" %COMMAND%
    )

    echo.
    title Command Prompt
    goto LOOP

:is_gui app_name
    set app_name=%~1
    set file_app=

    rem Get path of application
    if exist "%app_name%" (
        set file_app=%app_name%
    ) else (
        rem Lookup path of app
        for %%P in (%PATHEXT%) do (
          for %%I in (%1 %1%%P) do (
            if exist "%%~$PATH:I" (
              set file_app=%%~$PATH:I
              goto continue
            )
          )
        )
    )
    :continue
    if "%file_app%"=="" exit /b 1
    find /i "user32.dll" < "%file_app%" >nul 2>&1
    exit /b %errorlevel%
like image 161
William Bettridge-Radford Avatar answered Nov 09 '22 16:11

William Bettridge-Radford