My batchfile prompt keeps catching all answers as the first label - batch-file

My batch file always returns PLUGINS instead of going to the desired place when I press any number between those prompted. What could the problem stem from?
#echo off ECHO Control Panel initialized.
:BEGIN
CHOICE /N /C:1234 /M "PICK A NUMBER (1(PLUGINS), 2(MOTD), 3(LOGS),
4(END)"%1
IF %%ERRORLEVEL ==1 GOTO ONE
IF %%ERRORLEVEL ==2 GOTO TWO
IF %%ERRORLEVEL ==3 GOTO THREE
IF %%ERRORLEVEL ==4 GOTO END
:ONE
ECHO PLUGINS
explorer \\192.168.1.16\Server\Server-Dedicated Slightly\Here is the
Actual Server dir\plugins
GOTO BEGIN
:TWO
ECHO MOTD
notepad \\192.168.1.16\Server\Server-Dedicated Slightly\Here is the
Actual Server dir\plugins\Essentials\motd
GOTO BEGIN
:THREE
ECHO LOGS
notepad \\192.168.1.16\Server\Server-Dedicated Slightly\Here is the
Actual Server dir\server
GOTO BEGIN
:END
PAUSE

Three problems:
ERRORLEVEL checks for the given value, or above, so you have to order them from HIGH to LOW, not the other way around
it's ERRORLEVEL, not %%ERRORLEVEL
it's just ERRORLEVEL num
So, in short, you'll get
IF ERRORLEVEL 4 GOTO END
...
IF ERRORLEVEL 1 GOTO ONE

Related

Is it possible to create an interactive notepad within batch script that saves users input

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.

BATCH User input to be set as variable, be checked for existence at end of script and apply command

Should be straight forward I think, well I thought it was, but having some trouble getting it to work.
Aims:-
Create automated install using various batch commands, files and software installers. In order that the user doesn't have to complete the install, a set it and forget it affair, I want them to choose from 3 options at the start of the process BUT only get applied at the end, so the one of the 3 tasks they chose is done without user input.
It's all done just having problems with the start phase.
Code:
User can choose 3 things, copy a file and start the software, don't copy a file and launch a config tool, copy and file and don't run the software (waiting on additional prep).
SET /P "Input=Enter a Number Choice & Press RTN: "
if '%Input%'=='1' set end1=end1
if '%Input%'=='2' set end2=end2
if '%Input%'=='3' set end3=end3
Here is the start menu above, as you can see the number choice 'should' be setting a variable. After this is set, the file runs, installs and does things, then comes to the end where it should run the corresponding action defined by the start menu choice So...
IF exist %end1% GOTO end1
IF exist %end2% GOTO end2
IF exist %end3% GOTO end3
However it never detects anything other than just running the first of the choices by default. Ideally if the client pressed 1,2 or 3 at the beginning, when it comes the end, it goes to the menu option related where the appropriate action is launched;
:end 1 "copies a file" launches software
:end 2 "starts a program"
:end 3 "copies a file in preparation for some other task"
Hope that makes sense. Let me know if you need more info. I have searched but probably not searching for the right thing.
If I try and echo for the presence of end1 and it says it is not defined. I'm guessing maybe error level checks would be better, but struggling with that also.
Thank you...
EXIST checks for the existence of a file or folder. You want DEFINED. http://ss64.com/nt/if.html
I'm not sure if you really need to set 3 different variables though (maybe you've done it this way to reduce the issue to simplest steps). You can restructure it a bit like this:
SET /P "Input=Enter a Number Choice & Press RTN: "
if '%Input%'=='1' set ending=1
if '%Input%'=='2' set ending=2
if '%Input%'=='3' set ending=3
IF %ending% EQU 1 GOTO end1
IF %ending% EQU 2 (GOTO end2) ELSE (GOTO end3)
:end1
echo ending 1
pause
:end2
echo ending 2
pause
:end3
echo ending 3
pause
This code works for me.
OK been fiddling, this works:
SET /P "Input=Enter a Number Choice & Press RTN: "
if '%Input%'=='1' set ending=1
if '%Input%'=='2' set ending=2
if '%Input%'=='3' set ending=3
~things happen~
color 1f
IF %ending% EQU 1 GOTO end1
IF %ending% EQU 2 (GOTO end2) ELSE (GOTO end3)
All three menu options behave!
Assuming that end1, end2, end3 are defined elsewhere in the batch as folder paths, try this:
#echo off
set "mes1=Enter a Number Choice and Press RTN"
choice /c 123 /m "%mes1%" /t 10 /d 1
if %errorlevel% equ 3 (set disk=end3
) else if %errorlevel% equ 2 (set "disk=end2
) else (set disk=end1)
if exist "%disk%" (
if %disk%==%end1% (goto :end_1
) else if %disk%==%end2% (goto :end_2
) else if %disk%==%end3% (goto :end_3)
::some code here
:end_1 rem "copies a file" launches software
:end_2 rem "starts a program"
:end_3 rem "copies a file in preparation for some other task"
If you don't use drive paths, try this:
SET /P "Input=Enter a Number Choice & Press RTN: "
if '%Input%'=='1' (set ending=1
) else if '%Input%'=='2' (set ending=2
) else if '%Input%'=='3' (set ending=3)
if defined ending (GOTO :process%ending%) else (goto :end)
:: put some code here
:process1 rem "copies a file" launches software
:process2 rem "starts a program"
:process3 rem "copies a file in preparation for some other task"
:end
timeout 5 >nul
exit /b

Using Multiple GTR, LEQ, etc "if" statements in Batch

I have a batch file that asks the user to input a specific number. If the number is in between a given set of numbers, it goes to that label. However, say the user puts 100 it goes right to :smallsip
All in all, I'm trying to make it so that if the user types a number within a specific range (I.E 30-99) it goes to a specific label. Any suggestions?
:getadrink
cls
echo How many sips will Jackie Chan drink?
set /p numberofsips=Type Number of Sips Here:
if %numberofsips% LSS 0 goto waitwhat
if %numberofsips% GEQ 1 goto smallsip
if %numberofsips% GEQ 10 goto plenty
if %numberofsips% GEQ 30 goto toomuch
if %numberofsips% GEQ 100 goto waytoomuch
:waitwhat
cls
echo what
pause
:smallsip
cls
echo small sips
pause
:plenty
cls
echo plenty
pause
:toomuch
cls
echo too much!
pause
:waytoomuch
cls
echo WAY TOO MUCH
pause
P.S. I've been lurking around numerous posts on here, getting help for something I'm creating with Batch. Yes, I know batch is outdated, but I just seem to like it as I have discovered it about 2 months ago.
The if statements in your code work correctly, but your logic is wrong. For instance, when you type a number 50, the condition %numberofsips% GEQ 1 is already met, so the following if statements will never be reached. To solve this, simply reverse their order.
Another problem is, that you fall into fall into code which you do not want to be executed. For example, when the portion :smallsip has finished (and you confirmed the pause), execution continues at :plenty unintentionally. To avoid that, you will need a goto to jump somewhere else or an exit /B to leave the batch script.
Here is a fixed code:
:getadrink
cls
echo How many sips will Jackie Chan drink?
:askforsips
set numberofsips=0
set /p numberofsips=Type Number of Sips Here:
if %numberofsips% GEQ 100 goto waytoomuch
if %numberofsips% GEQ 30 goto toomuch
if %numberofsips% GEQ 10 goto plenty
if %numberofsips% GEQ 1 goto smallsip
goto waitwhat
:waitwhat
cls
echo what?
pause
goto askforsips
:smallsip
cls
echo small sips
pause
exit /B
:plenty
cls
echo plenty
pause
exit /B
:toomuch
cls
echo too much!
pause
goto askforsips
:waytoomuch
cls
echo WAY TOO MUCH
pause
goto askforsips
These are the things that I changed:
the order of if queries is reversed;
the if %numberofsips% LSS 0 query is removed, so :waitwhat is executed if the entered value is zero or less; in your code, :waitwhat was also executed in case the value was zero as none of the conditions were met; the final (lonely) goto waitwhat is not required here, but it is more obvious what happens;
a new label :askforsips is introduced to allow another user input in case an invalid value (zero or less) was given;
variable numberofsips is now reset before the user prompt, because set /P keeps the former value if the user just presses ENTER;
every section from :waitwhat down to :waytoomuch is terminated explicitly, either by goto askforsips or by exit /B;

How to mention If else condition in batch file

Is there any code system like below :
#echo off
set /p location=Type Folder Location
copy "%location%\file.txt" "c:\Folder"
if copy is done goto ok
if not goto failed
:ok
echo File is copyed succesfully
:failed
echo File is not copyed
echo.
pause
exit
#echo off
set /p location=Type Folder Location
copy "%location%\file.txt" "c:\Folder"
if errorlevel 1 goto failed
:ok
echo File is copied succesfully
goto done
:failed
echo File is not copied
:done
echo.
pause
exit
Normally, when a command succeeds, the "magic" variable errorlevel is set to zero; if it fails, to non-zero.
The syntax if errorlevel n will be true if errorlevel is n or greater than n (this last point is important - if errorlevel 0 will always be true (in normal circumstances).
Unlike many languages, batch has no concept of the end of a "procedure" - it simply continues execution line-by-line until it reaches the end-of-file. Consequently, you need to goto :eof after completing the mainline, otherwise execution will continue through the subroutine code. :EOF is a predefined label understood by CMD to mean end of file. The colon is required.
(in this case, the goto done skips over the 'failed' message - often you want to terminate the batch under certain circumstances. there you'd use goto :eof)
Like #Magoo saids in your case you just need 1 condition to go forward.
But to answer your question: How to mention If else condition in batch file
#echo off
set /a $var=1
if %$var%==1 (goto:ok) else (goto:nok)
:ok
echo OK
exit/b
:nok
echo NOK
You can change the value of $var to check it

Access Denied...?

#echo off
color 4
ping localhost 2.5 > nul
echo Welcome to the configuration menu.
echo Move AH1.exe and AH2.exe to your desktop.
echo This only works the first time
echo SO BE CAREFUL!
echo Would you like this to start on computer startup?
echo say "yes" or "no" below.
set /p option=Option:
IF %ERRORLEVEL% EQU no goto no
IF %ERRORLEVEL% EQU yes goto yes
:yes
echo Move AH1.bat and AH2.bat to desktop.
::Copies files to dekstop of current user *For Windows 7
xcopy "%systemdrive%\users\%username%\Desktop\AH1.bat" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup"
xcopy "%systemdrive%\users\%username%\Desktop\AH2.bat" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup"
echo Moved.
echo Press any key to close Anti Hacker Configuration.
pause > nul
exit
:no
echo No?The program will not take full effect then.
pause
exit
If you want to test,make 2 batch files on your desktop called AH1 and AH2
It says Access Denied....why??
No matter what you put into the option variable, it will always go to :yes.
I'm guessing you got mixed up with the choice command when you were making this because errorlevel doesn't contain the option you choose when you have the set command.
From the set documentation:
%ERRORLEVEL% - expands to the current ERRORLEVEL value
So, you might want to change if %errorlevel% equ no goto no to if %option% equ no goto no.
Also, what happens if you don't put either yes nor no. You need to account for these things by perhaps having a exit command after the second if statement.

Resources