I have this type of command but I want to print the result
command below:
ECHO Access to this system is restricted to authorized personnel for authorized business purpose only!!
CHOICE /M "Do you want to continue"
If Errorlevel 2 GoTo N
:Y
ECHO.
ECHO You choose: YES
set /p id=Enter Name/ID:
ECHO Hello %id%
timeout /t 5
GoTo ErrorFunc
:N
Echo.
Echo You choose: NO and you are required to exit from cmd !
Echo.
ECHO EXITING IN 5 SECOND
timeout /t 5
exit
test.bat > C:\test.log
If I type Y and print the result, it will stuck the result below:
Access to this system is restricted to authorized personnel for
authorized business purpose only!! Do you want to continue [Y,N]?
Any idea for a solution?
To add selections to your question, simply use
CHOICE /C:yn /M "Do you want to continue?"
if %ERRORLEVEL% EQU 1 GOTO Y
if %ERRORLEVEL% EQU 2 GOTO N
ERRORLEVEL 1 indicates the first selection which is /C:yn, ERRORLEVEL 2 indicates the second selection which is /C:yn.
Related
I'm making a batch file game (I know it's not really for games). I need to run set /p to ask the user to choose which option to do while it adds a certain amount to their in game money every second, but I can't figure out how to run it at the same time.
I've tried using it in a for loop, but I can't get it to work.
Here is an example:
:do
::echo choices and ask user which option to do
echo choice 1
echo choice 2
echo choice 3
set /p choice= what to do:
if "%choice%" == "1" goto c1
if "%choice%" == "2" goto c2
if "%choice%" == "3" goto c3
::add money
set /a "money=%money%+10"
::wait one second
ping localhost -n 2 >NUL
::do it all again
goto do
I want it to give them money even if they don't choose an option, so they get their money every second.
You may have to use CHOICE because SET /P will read user input until you gives it a new line.
CHOICE has a parameter /T, which can tell you after n seconds will select default choice (use /D <default key> to set default choice). For example, choice /c 123y /t 1 /d y will wait input for 1 second, and only select the last choice (which is y) if user does not pressing anything for 1 second. You can also display custom prompt message by adding /M "<message>", so your complete command is:
choice /c 123y /t 1 /d y /m "what to do:"
%ERRORLEVEL% is where CHOICE command gives output. It's starts from 1 to the last character index (in this case, y will be 4):
if "%errorlevel%"=="1" goto c1
if "%errorlevel%"=="2" goto c2
if "%errorlevel%"=="3" goto c3
rem The last one, which is 4, but because it's the only last option, so you don't need another if statement
And in the end of the loop, you may want to add money:
rem Instead of "set /a money=%money% + 10", why you don't use += instead?
set /a money+=10
goto theBeginOfTheLoop
Combine those statements together, we'll have this simple goto loop:
:theBeginOfTheLoop
echo money: %money%
echo choice 1 [1]
echo choice 2 [2]
echo choice 3 [3]
choice /c 123y /t 1 /d y /m "what to do: "
if "%errorlevel%"=="1" goto c1
if "%errorlevel%"=="2" goto c2
if "%errorlevel%"=="3" goto c3
set /a money+=10
goto theBeginOfTheLoop
Have a nice day with Batch :)
I'm in the process of creating a mini-game using batch and one of the useful tool ideas was to have an interactive notepad so that users could store information throughout the game and refer back to it later. So far I have created the option to goto a notepad within an In-game pause menu but wasn't sure if it was possible to save results without outputting to new file on the desktop
:PauseMenu
cls
echo.
echo %Alias%
echo.
echo Notepad
echo Stats
echo Untitled2
echo Untitled3
echo Untitled4
echo Untitled5
echo Untitled6
set/p PauseMenu="N, S"
IF ["%PauseMenu%"]==["N"] goto Notepad
IF ["%PauseMenu%"]==["S"] goto Stats
IF ["%PauseMenu%"]==["N"] goto
IF ["%PauseMenu%"]==["N"] goto
IF ["%PauseMenu%"]==["N"] goto
IF ["%PauseMenu%"]==["N"] goto
IF ["%PauseMenu%"]==["N"] goto
Any help would be appreciated, thanks.
PS is it possible to go back to the previous page from a menu?
Simplicity itself.
First, some renaming may be in order. notepad is a supplied utility and pausemenu is being used both as a variable and as a label. This is not invalid, but can be a little confusing.
Further, if you are choosing between a set of keys, I'd suggest you investigate choice. choice has a number of advantages, like it only accepts one character, no enter is required and it's not necessary to analyse the entry.
So: revising your code:
:p_pausemenu
pause
:PauseMenu
cls
echo.
echo %Alias%
echo.
echo N Notepad
echo S Stats
echo 1 Untitled2
echo Z Untitled3
echo Q Untitled4
echo J Untitled5
echo X Untitled6
:: Note that the processing of ERRORLEVEL must be in reverse order
choice /c ns1zqjx
if errorlevel 7 goto labelx
if errorlevel 6 goto labelj
if errorlevel 5 goto labelq
if errorlevel 4 goto labelz
if errorlevel 3 goto label1
if errorlevel 2 goto stats
if errorlevel 1 goto unotepad
:unotepad
start "Notes for %alias%" notepad "c:\gamedirectory\%alias%.txt"
goto pausemenu
:stats
:: List your stats here
echo Stats for %alias%
goto p_pausemenu
Here, a menu with a number of unimplemented options is presented and the choice command (see choice /? from the prompt for more options) waits for a choice to be made.
errorlevel is set according to the choice made - but since if errorlevel n means if errorlevel is n OR GREATER THAN n you need to process errorlevel in reverse order.
Then each selection is processed. n will start a notepad instance and load the alias.txt file from the game directory, then present the menu again as it returns to pausemenu. s will show the stats (idk what you need for that) and then return to p_pausemenu which will pause and then proceed to show the menu when the user signals to do so.
My batch file skips specific loops and just does certain loops (it only does the first three loops and ignores the rest).
I tried rewriting the batch file, incorporating the new code into an old existing batch file I wrote, but I can't get it working.
echo 1. Login
echo 2. Exit
echo.
set /P M = Command :
IF M == 1 GOTO 1
:1
cls
echo Login
echo.
Set Pass1 = MCTh3sn3r
Set /P Pass = Enter Password :
IF Pass == %Pass1% GOTO 2
:2
cls
echo.
echo Login Successful GOTO 4
:3
cls
echo.
echo Incorrect Password
:4
echo.
echo 1. Shutdown (S)
echo 2. Reboot (R)
echo 3. Logoff (L)
echo 4. Task Manager (TM)
echo 5. Command Prompt (CP)
echo 6. File Locker (FL)
echo. Set /P Choice = Command :
IF Choice == "S" GOTO 5
IF Choice == "R" GOTO 6
IF Choice == "L" GOTO 7
IF Choice == "TM" GOTO 8
IF Choice == "CP" GOTO 9
IF Choice == "FL" GOTO 10
:5
cls
echo.
echo System will now shutdown
Timeout /t 5
Shutdown /s
GOTO End
:6
cls
echo.
echo System will now reboot
timeout /t 5
Shutdown /r
GOTO End
:7
cls
echo.
echo System will now logoff
timeout /t 5
Logoff
GOTO End
:8
gpupdate /force
gpupdate /force
gpupdate /force
gpupdate /force
start taskmgr
GOTO End
:9
gpupdate /force
gpupdate /force
gpupdate /force
gpupdate /force
start cmd
GOTO End
:10
(
if EXIST "PrivateFolder" goto
11
if NOT EXIST Locker goto 12
:13
echo Are you sure you want to
lock the folder (Y/N) ?
set /p Choice = Command :
IF Choice == "Y" goto 14
IF Choice == "y" goto 14
IF Choice == "N" goto 15
IF Choice == "n" goto 15
echo Invalid Choice goto 13
:16
ren Locker "PrivateFolder"
attrib +h + s "PrivateFolder"
echo Folder Locked GOTO End
:11
echo Enter password to unlock
file :
set /p Pass = Password :
IF NOT %Pass% == MCTh3sn3r goto
17
attrib -h -s "PrivateFolder"
ren "PrivateFolder" Locker
echo File locked successfully.
GOTO End
:17
echo Invalid password.
GOTO End
:12
md Locker
echo Locker created
successfully.
GOTO End
)
:End
Exit
Well lets start at the TOP.
This set of code has three things wrong with it.
Do not leave a space between your variable names and variable assignments.
When you need to use a variable you need to surround it with percent symbols.
Your GOTO command will GOTO label 1 regardless of the variable M equaling 1.
.
echo 1. Login
echo 2. Exit
echo.
set /P M = Command :
IF M == 1 GOTO 1
:1
This code should really look something like this.
echo 1. Login
echo 2. Exit
echo.
set /P M=Command :
IF "%M%"=="1" (
GOTO 1
) else (
GOTO END
)
:1
You make the same mistakes in the next set of code. Not sure why you chose to use the percent symbols for one variable but not the other. Again, you need to close up the spaces, use percent symbols for all your variables and your GOTO will go to the label 2 regardless so you need some other code to branch to.
cls
echo Login
echo.
Set Pass1 = MCTh3sn3r
Set /P Pass = Enter Password :
IF Pass == %Pass1% GOTO 2
:2
So again, using my best coding practices I would code it like this.
:1
cls
echo Login
echo.
Set Pass1=MCTh3sn3r
Set /P Pass=Enter Password :
IF "%Pass%"=="%Pass1%" (
GOTO 2
) else (
Echo Wrong Password
Pause
GOTO 1
)
Just going to keep repeating myself. Same problems. Close up the spaces and use percent symbols with your variables. Might want to throw in a case insensitive check for their choices on this one. Not sure how you thought their answers were ever going to match here. You have quotes surrounding one side of your comparison but not the other. Not even going to correct this code you should understand by now what you are doing wrong.
echo. Set /P Choice = Command :
IF Choice == "S" GOTO 5
IF Choice == "R" GOTO 6
IF Choice == "L" GOTO 7
IF Choice == "TM" GOTO 8
IF Choice == "CP" GOTO 9
IF Choice == "FL" GOTO 10
No clue why you are using parentheses here.
:10
(
And here.
)
:End
Exit
This one is dead obvious. You can't have your label on another line.
if EXIST "PrivateFolder" goto
11
And here again. Close up the spaces. Put quotes on both sides of your comparisons. Use percent symbols to reference a variable. And read the HELP for the IF command. Use the /I option!
:13
echo Are you sure you want to
lock the folder (Y/N) ?
set /p Choice = Command :
IF Choice == "Y" goto 14
IF Choice == "y" goto 14
IF Choice == "N" goto 15
IF Choice == "n" goto 15
echo Invalid Choice goto 13
While we are here I would advise that if you are going to keep using the same variable over and over and over again that you clear out the variable before you ask the question. Make this change above.
set "Choice="
set /p Choice=Command :
Couple of problems here. The word file: is on its own line. The number 17 is on its own line. You finally realized that you needed to use percent symbols for you variables but the variable %Pass% is not the same as the variable %Pass %. Close up the spaces and use quotes.
echo Enter password to unlock
file :
set /p Pass = Password :
IF NOT %Pass% == MCTh3sn3r goto
17
So I'm making a game based on making decisions using notepad ++ and batch. I'm new to this and I have no idea what the problem is here.
:start
;startup
cls
echo Wine or Cheese?
echo.
echo 1. Wine
echo 2. Cheese
echo 3. Exit
echo.
set /p input0=Enter:
if %input0% equ 1 goto winestart
if %input0% equ 2 goto cheese
if %input0% equ 3 goto exit
When I select winestart or 1, it goes to winestart for a splitsecond then crashes, with the error message: 1 was unexpected at this time.
Winestart looks like this:
:winestart
cls
echo You are alone. You have a bottle of wine and the clothes you are wearing.
echo What kind of wine do you have?
echo.
echo 1. Red Wine
echo 2. White Wine
set p/ input7=Enter:
if %input7% equ 1 goto redwine
if %input7% equ 2 goto whitewine
/p indicates that the variable is simply set by user input. –
SET
:winestart
cls
echo You are alone. You have a bottle of wine and the clothes you are wearing.
echo What kind of wine do you have?
echo.
echo 1. Red Wine
echo 2. White Wine
set /p input7=Enter:
if %input7% equ 1 goto redwine
if %input7% equ 2 goto whitewine
[Edit: Correct cure, but no explanation of why the error message appears]
Since input7 is not entered, the if statement is interpreted as
if equ 1 goto redwine
cmd expects if something equ anotherthing dosomething so it sees 1 as the comparison-operator. This is not one of the operators it recognises, so it responds that the 1 is unexpected.
A better form of this is
if /i "%input7%" equ "1" goto redwine
where the /i forces a case-insensitive comparison and "quoting the arguments" preserves the syntax requirement where the user enters nothing or a string containing spaces (you have no control over the user's response.)
Please help to prepare batch script on DOS.
This script should run in following manner :
Telent the IP and use the existing passwd (explicitly given in the script).
After the telnet, it shows MENU options
MB station
RC
ODU
AP
SU
Exit
type 1 // a "MB station" MENU options will open i.e.
1 - Show
2 - Unit Control
type 2 // UC MENU options will open i.e
1 - Change Password
2 - Reset
type 1 //change passwd MENU options will open i.e.
1 - Change PC Password
2 - Change LU Password
3 - Change Admin Password
type 3 // to change ADMIN passwd
MB station - Change Admin Password
Enter New Password : XYZ enter
Re-enter Password : XYZ enter
New password accepted
3 times escape // to escape from telnet
1.MB station
2. RC
3. ODU
4. AP
5. SU
6. Exit
type 6 // to exit
Exit? [Y/N] y
Connection to host lost.
then move to step with different IP. The IP values will be given by the user one-time while executing the script at the prompt e.g. ./pass-change IPs.txt
start with this :
#ECHO OFF
:BEGIN
CLS
ECHO.
ECHO Oleg Grishko + Flora = Love
ECHO.
ECHO.
ECHO 1=Remove All Hard Drive Partitions
ECHO 2=FDISK Hard Drive
ECHO 3=Format Hard Drive
ECHO 4=Dell Utilities
ECHO 5=Re-image
ECHO 6=Exit To DOS
ECHO.
ECHO To bring this menu back type GO.BAT at the dos prompt.
ECHO.
CHOICE /N /C:123456
ECHO.
If ERRORLEVEL ==6 GOTO SIX
If ERRORLEVEL ==5 GOTO FIVE
If ERRORLEVEL ==4 GOTO FOUR
IF ERRORLEVEL ==3 GOTO THREE
IF ERRORLEVEL ==2 GOTO TWO
IF ERRORLEVEL ==1 GOTO ONE
GOTO END
:SIX
EXIT
GOTO QUIT
:FIVE
call re-image.bat
GOTO END
:FOUR
call dell.bat
GOTO END
:THREE
cd dos
call format.bat
GOTO END
:TWO
cd dos
call fdisk.bat
GOTO END
:ONE
cd dos
call blast.bat
GOTO END
:END
cd\
REM ECHO Completed. Bringing up DOS menu again...
REM pause
REM f:go.bat
:QUIT
f: