Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was bored so I tried to make a virtual console inside CMD and it appears to be working fine up until the point of checking the input and executing a command accordingly.

I get a screen that says ": " and there you can type what command you want to execute, and typing works fine but when it goes to check what command to execute, problems occurr.

The problem is if I type "register" which should go to a label called :reg, I execute the "exit" command, which should only be executed when I type "exit".

Here's my script:

echo off
cls

:console
title Command prompt
cls
set /p c=": "

if "%c%"=="exit"
(
exit
)

if "%c%"=="register"
(

goto :reg

)


:reg
title Register a new account
cls
echo Registration
echo.

set /p newName="Username: "
set /p newPass="Password: "
cls
echo Processing...
>nul timeout /t 3
goto console


What I have tried:

I've tried changing the order of the commands and if-statements but it appears that the first command always gets executed no matter what you type.
Posted
Updated 11-Oct-16 5:18am

Try this, stuck pauses in and echo to verify:

cls
 
:console
title Command prompt
cls
set /p c=": "
echo %c%
pause

if "%c%"=="exit" goto end
 
if "%c%"=="register" goto reg

pause 

:reg
title Register a new account
cls
echo Registration
echo.
 
set /p newName="Username: "
set /p newPass="Password: "
cls
echo Processing...
>nul timeout /t 3
goto console
:end
 
Share this answer
 
v3
Remove the quotes from everything and there's no need for brakets

echo off
cls
 
:console
title Command prompt
cls
set /p c=: 
 
if %c%==exit exit
 
if %c%==register goto :reg

 
:reg
title Register a new account
cls
echo Registration
echo.
 
set /p newName="Username: "
set /p newPass="Password: "
cls
echo Processing...
>nul timeout /t 3
goto console
 
Share this answer
 
Where did you catch that if is multi line ? and with () ?
Here is a link for the syntax:
More Powerful Batch Files- Branching with "If" statements[^]

Windows Command Line |Shell List and Reference[^]
 
Share this answer
 
if "%c%"=="register"
(
goto :reg 
)

:reg

See anything wrong there? If the user does not type exit, then anything else will go to the :reg label. Make your subroutines into proper subroutines by using call not goto.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900