I have a fairly complicated batch file that is like a custom command prompt.
I have it to prompt the user for a command, then if the command contains "start" it goes to a place in my code like this:
%command%
So if you wrote "start iexplore" it starts Internet explorer. The problem is, if you write "start dsfgiusdhfpgiuahdfuoavphc" or a program that doesnt exists, it just closes my file!! Help!
BINGO it works: set /p "command=C:\>" goto CONTAINCHECK :CONTAINCHECK if "%command%"=="%command:start=%" ( goto CONTAINCHECK2 ) ELSE ( goto GOTO2 ) :CONTAINCHECK2 if "%command%"=="%command:goto=%" ( echo Command not recognized... goto COMMANDPRO ) ELSE ( goto GOTO ) :GOTO SET result=%command:~5% ECHO Calling %result% CALL :%result% 2>NUL || ECHO The code section %result% was not found... goto COMMANDPRO :GOTO2 echo PIZZAL goto COMMANDPRO :TEST echo HELLO goto COMMANDPROI can now go to any part of my program!
But how can I make a similar thing to check if the entered PROGRAM exists?
Tailor accordingly: @ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION :: _Command = command/program to find :: _Command_Path = variable name to contain path to _Command :: Find_Program = subroutine to find command/program SET /P _Command=Enter command: CALL :Find_Program %_Command% _Command_Path IF DEFINED _Command_Path ( ECHO Command: "%_Command%" is found at this path: %_Command_Path% ) EXIT /B :Find_Program ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :: This subroutine is used to determine path information. The first passed :: :: argument is searched for in HELP to determine if it is a command. If it :: :: is a command, the path variable value is set to the command name only. :: :: Next, the first passed argument is searched for in the %path% variable. :: :: If it is found in the system path, the path variable is set to the :: :: program name only. :: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: FOR /F "DELIMS=" %%A IN ('HELP ^|FINDSTR /I /B "%~1" ^|FIND /I /C "%~1"') DO ( SET /A CommandFound=%%A IF !CommandFound! EQU 1 ( SET %2=%~1 GOTO :EOF ) SET ProgramFound=%~$PATH:1 IF DEFINED ProgramFound ( SET %2="!ProgramFound!" GOTO :EOF ) ECHO "%1" not found! ) GOTO :EOFWhen your only tool is a hammer, every problem looks like a nail.
I don't understand.... Sorry! Would you like me to give you more of my code so that you can prepare it so that I dont have to change anything?
set /p "command=C:\>" goto CONTAINCHECK :CONTAINCHECK if "%command%"=="%command:start=%" ( goto CONTAINCHECK2 ) ELSE ( goto GOTO ) :CONTAINCHECK2 if "%command%"=="%command:goto=%" ( echo Command not recognized... goto COMMANDPRO ) ELSE ( goto GOTO ) :GOTO if %command% exists ( %command% )
My problem is on the line "if %command% exists ( "
What am I supposed to do there to check if the entered program exists "start ____" or if the section exists "goto ____" ?
It's not a good idea to use variables and/or labels that are native commands or programs to the OS (such as command and goto). I'm afraid you'll just end up confusing the interpreter and end up with bizarre, unexpected, and undesireable results. What am I supposed to do there to check if the entered program exists "start ____" or if the section exists "goto ____" ?
You'll need to parse what the user has input, and pass anything after "start" to the Find_Program subroutine I provided, and pass anything after "goto" to something similar to this:
CALL :%command% 2>NUL || ECHO Label %command% not foundThe CALL command is a bit more tolerant of missing labels than the GOTO command.
When your only tool is a hammer, every problem looks like a nail.
Could I use this to extract "goto "? SET _result=%_test:~0,5%
ECHO %_result%
BINGO it works: set /p "command=C:\>" goto CONTAINCHECK :CONTAINCHECK if "%command%"=="%command:start=%" ( goto CONTAINCHECK2 ) ELSE ( goto GOTO2 ) :CONTAINCHECK2 if "%command%"=="%command:goto=%" ( echo Command not recognized... goto COMMANDPRO ) ELSE ( goto GOTO ) :GOTO SET result=%command:~5% ECHO Calling %result% CALL :%result% 2>NUL || ECHO The code section %result% was not found... goto COMMANDPRO :GOTO2 echo PIZZAL goto COMMANDPRO :TEST echo HELLO goto COMMANDPROI can now go to any part of my program!
But how can I make a similar thing to check if the entered PROGRAM exists?
But how can I make a similar thing to check if the entered PROGRAM exists? Reply #1 provides that capability. If you are not going to require program extensions from the user, it would be good to loop through the %PATHEXT% environment variable to test for the existence of the different kinds of executable.
When your only tool is a hammer, every problem looks like a nail.
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |