Reputation: 55
I want to know if there any solution to this:
Main.bat:
@echo off
goto 'input'
: 'input'
cls
set "inp="
set /p inp=What would you like to do?
set firstresponse=%inp:~0,5%
if %firstresponse%==help goto 'help'
pause
if /I %firstresponse%==check set firstresponse=dir && set
executeparttwo=%inp:~5%
if /I %firstresponse%==remov goto 'remove'
%firstresponse%%executeparttwo%
pause
goto 'input'
: 'remove'
set "firstresponse=" && set firstresponse=%inp:~0,6%
if /I %firstresponse%==remove set firstresponse=del
set executeparttwo=%inp:~6%
%firstresponse%%executeparttwo%
pause
goto 'input'
: 'help'
cls
echo Check = Dir in regular command prompt, checks a directory.
echo Remove = del in regular command prompt, deletes something.
pause
goto 'input'
if the User typed an invalid command, it will show like what CMD does ( 'command' is not recongnized...)
What i want to do is to replace the CMD invalid command text to my own one like "command" is an invalid command, but to do that i need to "hide" the CMD one (because if the user typed a invalid command it will not show him a "custom message")
I tried to use some Batch plugins like batbox, CursorPos etc... To replace the cursor position but i didn't get what i wanted. So if anyone have a solution i will be very appreciated!
Upvotes: 1
Views: 4502
Reputation: 56180
Your splitting the command and parameters is not ideal, there is a much easier and safer way. Also, the method of an own subroutine for each command is suboptimal (especially, when you add more and more commands).
@echo off
call :commandlist REM build translation table
:input
REM get input line:
set /p "commandline=Enter Command: "
REM split to command and parameters
for /f "tokens=1,*" %%a in ("%commandline%") do (
set "command=_%%a"
set "params=%%b"
)
REM check for valid command:
set _|findstr /bi "%command%=" >nul || (
echo invalid command: '%command:~1%'.
goto :input
)
REM execute the command:
call %%%command%%% %params%
goto :input
:Commandlist
set "_check=dir /b"
set "_remove=del"
set "_help=:help"
set "_where=call echo %%cd%%"
set "_change=cd"
set "_apt-get=:apt"
set "_bye=exit /b" 'secret' exit command ;)
goto :eof
:help
echo Check = Dir in regular command prompt, checks a directory.
echo Remove = del in regular command prompt, deletes something.
echo Where = echo %%cd%% in regular command prompt, print working folder.
echo Change = cd in regular command prompt, change working folder
goto :eof
:apt
if /i "%~1" == "update" echo updating... & goto :eof
if /i "%~1" == "whatever" echo whatevering... & goto :eof
echo invalid command: '%command:~1% %1'
goto :eof
(Note to experienced batch users: yes I know there is a possibility for some "code injection")
Upvotes: 1