Ok First have all i'm sorry for my poor english :) Now for my problem
I'm trying to create a Script file with multiple choices and I ran into a problem. If I don't enter an input (just press enter) the file displays an error and shuts down. now My question is whether t开发者_开发知识库here is a command that will force the file to go back to the Start in case where no input is entered?
This relevant part of my script
:input
@echo off
CLS
ECHO #############################################
ECHO # #
ECHO # 1 - Make A System Folder or File #
ECHO # 2 - Restore Folder or File #
Echo # 3 - Exit #
ECHO # #
ECHO #############################################
SET /P O=Set Your Choice And Press Enter:
ECHO Loading .........
(
IF %O%==1 GOTO Hide
IF %O%==2 GOTO Show
IF %O%==3 GOTO Exit
IF %O%=="" GOTO input
)
ELSE (
GOTO input
)
As you can see I tried to use "Else" but it works only partially. If I enter a variable that is not part of the list given (in my case 1, 2, 3) the File gose back to the beginning, and even if i send non input request afterword the file keep working properly. The problem is very specific, Only when the first input is a non input the problem Occurs. in any other situation the script works fine
Thank you and Again I'm very sorry for my poor English :)
The parenthesis are not necessary. Try this,
:input
@echo off
CLS
ECHO #############################################
ECHO # #
ECHO # 1 - Make A System Folder or File #
ECHO # 2 - Restore Folder or File #
Echo # 3 - Exit #
ECHO # #
ECHO #############################################
SET /P O=Set Your Choice And Press Enter:
ECHO Loading .........
IF %O%==1 GOTO Hide
IF %O%==2 GOTO Show
IF %O%==3 GOTO Exit
IF %O%=="" GOTO input
ELSE
GOTO input
As a side note your English is very good!
Always reset variable before input. When user just presses enter variable doesn't reset automatically.
:: Delete variable %O% SET "O=" SET /P "O=Set Your Choice And Press Enter: "
Always take the variables in quotes (in IF)
IF "%O%"=="1" GOTO Hide IF "%O%"=="2" GOTO Show IF "%O%"=="3" GOTO Exit
GOTO Label
never returns back (exception -goto :label
in some crazy cases). In your case you can write:IF "%O%"=="1" GOTO Hide IF "%O%"=="2" GOTO Show IF "%O%"=="3" GOTO Exit GOTO INPUT
Add after each 'procedure' command
exit /B
to prevent crazy parsing after code changing. Warning:goto
command doesn't work likevoid
in C++, so commandexit /B
will stop script. But if you are usingcall :label
commandexit /B
will stop code only under:label
.
Yeah, if you want to go to the :start or :anything in case of no input, a solution is write the following:
this first line is the key
set num=""
set /P num=%input%
if %num%==1 goto Choice1
if %num%==2 goto Choice2
if "%num%"=="" goto Start
goto Start
This last line is for inputs outside of your range of choice (in this example, 3 would be invalid and go to the Start again
There is a better and easier solution though!
This is just a "more professional" solution to multiple choice menus, and it is REALLY useful...
Instead of setting %input% and all that jazz, just use choice! If you enter a value outside of the range (123 means you can choose 1 or 2 or 3), there will be "beep" that means invalid entry.
set errorlevel=
CHOICE /C 123 >nul
if %errorlevel%==1 goto Choice1
if %errorlevel%==2 goto Choice2
if %errorlevel%==3 goto Choice3
And that is it! So much easier, right? So just to explain what the lines mean...
The first line just sets the errorlevel to blank, because when you get more advanced, the errorlevel may be assigned a value unexpectedly. So just save yourself a headache later and erase it.
The >nul "hides" the fact that it's a choice. Just put an echo message above it to say "Press number X for Choice X". I love this because you don't need to press enter afterwards- saving time if you have lots of choices
Now, the errorlevel is not assigned your input...so watch out!
My choices could be "CHOICE /C 3689 >nul ", but the errorlevel output is still errorlevel=1 for entering 3, errorlevel=2 for entering 6, and so on.
So if you have an option to go back to the main menu by pressing 0, and CHOICE /C 0123, pressing zero will be errorlevel=1, pressing 1 will be errorlevel=2... etc.
Choice is super useful, just go and type choice /? for more details.
I tried
@echo off
title (title)
:c
echo ==============================
echo = 1=Create Folder/File =
echo = 2=File Restore =
echo = 3=Exit =
echo ==============================
:a
echo Press space-Enter for nothing
set /p 0=What do you want to do? :
if /i "%0%"=="1" goto 1
if /i "%0%"=="2" goto 2
if /i "%0%"=="3" goto 3
if /i "%0%"==" " goto h
PAUSE
:1
set /p key=What do you want your folder name to be? :
cd
Mkdir %key%
PAUSE
:2
set /p p=Whats the name of the file you want to restore? :
chkdsk %p%
PAUSE
:3
echo Exiting.
ping localhost -n 2
cls
echo Exiting..
ping localhost -n 2
cls
Echo Exiting...
ping localhost -n 2
exit /b
:h
set /p YN=Go back? (Y,N)
if %YN%=="Y" goto a
if %YN%=="N" goto 3
PAUSE
精彩评论