Check if program or section exists

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sebb
Posts: 5
Joined: 20 Apr 2012 17:14

Check if program or section exists

#1 Post by sebb » 08 Jun 2012 05:05

Hello I am fairly new to bach and I was wondering how I could make some code that checks if what you entered in a variable exists before trying it. So I made some code like this:

Code: Select all

set /p "command=C:\>"
if "%command%"=="%command:start=%" (
    goto CONTAINCHECK2
) ELSE (
    goto GOTO
)
:CONTAINCHECK2
if "%command%"=="%command:goto=%" (
    echo Command not recognized...
    goto COMMANDPRO
) ELSE (
    goto GOTO
)
:GOTO
%command%
goto COMMANDPRO

It checks if the entered variable contains "goto" or "start", if yes it starts an entered program (Ex. start iexplore) or it goes to a section of my code (Ex. goto START). The problem is, if you enter a program that does not exist, or if you want to go to a section of code that doesnt exists, it just closes my batch file... How could I make that it checks if that section or program exists before going to :GOTO and if it doesnt exists I want it to go to a section ":GOTONOT". Any help would be very appreciated!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Check if program or section exists

#2 Post by foxidrive » 08 Jun 2012 06:04

Here are some ideas. You will have to search the path for the executable if they don't specify a path to it.

Code: Select all

:begin
set /p "command=C:\>"

echo "%command%" |findstr /i "start goto">nul && goto :start

if not exist "%command%" (
    echo Command not recognized...
    goto COMMANDPRO
)

%command%
goto :begin
:start
start "" "iexplore.exe"

Post Reply