View unanswered posts | View active topics It is currently 16 Apr 2014 02:10



Post new topic Reply to topic  [ 2 posts ] 
Check if program or section exists 
Author Message

Joined: 20 Apr 2012 17:14
Posts: 5
Post Check if program or section exists
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:
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!


08 Jun 2012 05:05
Profile

Joined: 10 Feb 2012 02:20
Posts: 3640
Post Re: Check if program or section exists
Here are some ideas. You will have to search the path for the executable if they don't specify a path to it.

Code:
: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"


08 Jun 2012 06:04
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 


Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.