Search code examples
batch-fileif-statementechogotopause

How to make custom commands with batch?


I am working on making my own custom command prompt of sorts to make it easier to get things done. I've successfully created the interface and a type command, but when I tried adding a help command, it echoes the menu, but my pause >nul i put in doesn't work, the window just closes. Can you take a look, and help me out? Thanks in advance!

@echo off
:HELLO
echo Hello!
pause >nul
cls
echo What can I do for you? Type in your commmand! (Type "help" to display a list of every command available.)
set /p command=
if %command%==type (
goto :TYPEFILENAME
)
if %command%==help (
goto :HELPLIST
)
:MAIN
cls
echo What can I do for you? Type in your commmand! (Type "help" to display a list of every command available.)
set /p command=
if %command%==type (
goto :TYPEFILENAME
)
if %command%==help (
goto :HELPLIST
)
pause >nul
:HELPLIST
echo ----------------------------------
echo Help: Page 1/1
echo Command | What it does
echo help    | displays this page
echo type    | types a file
echo ----------------------------------
pause >nul
goto :MAIN

If you need the rest of the code, tell me! This is what I think is most important, so I didn't add in the rest.


Solution

  • The thing why the Help part is crashing, is the bar (|) character. When you want to echo special like this, you have to add an escape character (^) in front of them. That part of your code should be like this:

    echo ----------------------------------
    echo Help: Page 1/1
    echo Command ^| What it does
    echo help    ^| displays this page
    echo type    ^| types a file
    echo ----------------------------------