I created script for install files from network place.
However, I need to fix inputs from command line.
set num=
set /p num=Enter your choose:
if "%num%"=="1" goto installJabber
if "%num%"=="2" goto installTimer
if "%num%"=="3" goto installVMWare
if "%num%"=="4" goto installOffice
How do I write "if "%num"" is not 1 or 2 or 3 or 4 then exit ?
....
if "%num%"=="4" goto installOffice
goto :eof
:eof is recognised by batch as meaning 'the end of the batch file'. The colon is required.
Related
How do I call multiple batch files within a single batch? When I try it always goes to the same one or none at all and closes window.
#echo off
:MENU
title MENU0
Echo 1 - Select Menu 1
Echo 2 - Select Menu 2
Echo 0 - Exit
Echo.
SET /P choice=Type the number or letter of task you want, then press ENTER:
IF %choice%==1 GOTO 1
IF %choice%==2 GOTO 2
IF %choice%==0 EXIT
:1
call %userprofile%\desktop\\Menu1.bat
:2
call %userprofile%\desktop\Menu2.bat
There are several issues with provided batch code in question.
The first one is that after processing of the batch file called with command CALL finished, the processing of current batch file continues with the next command respectively line, except the called batch file contains itself the command EXIT without parameter /B as in this case the command processor terminates itself independent on calling hierarchy.
For details about CALL behavior see answers on:
How to call a batch file in the parent folder of current batch file?
In a Windows batch file, can you chain-execute something that is not another batch file?
The second issue is that folder path assigned to environment variable USERPROFILE could contain 1 or more spaces (default on Windows 2000/XP, possible on later Windows versions depending on user name). Therefore always enclose a string referencing USERPROFILE or USERNAME in double quotes.
The third and most difficult to handle issue is that the user of a batch file on prompt with set /P has the freedom to enter anything and not just what the writer of the batch file suggests.
For example
SET /P choice=Type the number or letter of task you want, then press ENTER:
IF %choice%==1 GOTO 1
results in an exit of batch processing caused by a syntax error if the batch user hits just RETURN or ENTER without entering anything at all and the environment variable choice is not already defined with a useful string because in this case the next line to process by command processor is:
IF ==1 GOTO 1
It is good practice to define the environment variable with a default value before set /P as this value is kept when the batch user just hits RETURN or ENTER.
A batch user has also the freedom on using set /P to enter anything including syntax critical characters like " or < or | or > and others by mistake or intentionally (for breaking batch processing by a syntax error).
Therefore it is in general better for menus in batch files to use the command choice (Microsoft article) because then the batch user can enter only what the writer of the batch file offers. But CHOICE is available only by default for Windows Server 2003 and later Windows. And there are different versions of choice (SS64 article with additional information) with a different set of options. So it depends on which Windows version(s) the batch file is designed for if CHOICE can be used at all.
It is also not good to name an environment variable or a label like a command although possible. Therefore choice is not a good name for an environment variable.
Here is a commented batch file with a code which avoids all those issues.
#echo off
:MainMenu
setlocal EnableDelayedExpansion
title MENU0
cls
echo 1 - Select Menu 1
echo 2 - Select Menu 2
echo 0 - Exit
echo.
rem Define 0 as default value in case of user just hits RETURN or ENTER.
set "UsersChoice=0"
set /P "UsersChoice=Type the number or letter of task you want, then press ENTER: "
rem Has the user really entered just one of the offered characters?
rem There must be nothing to process if the user has entered just 0
rem or 1 or 2. Otherwise the user's choice was either by mistake or
rem intentionally entered wrong. The string entered by the user is
rem referenced with delayed expansion to avoid an exit of batch
rem processing in case of user entered a syntax critical character.
for /F "tokens=1 delims=012" %%I in ("!UsersChoice!") do (
endlocal
goto MainMenu
)
rem Now it is safe to reference the variable value without usage of delayed
rem expansion as a syntax error caused by user input can't occur anymore.
rem The entered string does not contain any not expected character. But
rem it is possible that for example 11 was entered by mistake instead
rem of just 1. The entered string should have a length of 1 character.
if not "%UsersChoice:~1,1%" == "" (
endlocal
goto MainMenu
)
rem Exit this batch processing on user entered 0. Previous environment is
rem automatically restored by command processor by an implicit endlocal.
if "%UsersChoice%" == "0" exit /B
rem Restore previous environment as the called batch files are most
rem likely written for using standard command environment with delayed
rem expansion not enabled (exclamation mark interpreted different).
rem The current value of local environment variable must be passed
rem to previous environment for usage on GOTO command.
endlocal & goto Menu%UsersChoice%
:Menu1
call "%USERPROFILE%\Desktop\Menu1.bat"
goto MainMenu
:Menu2
call "%USERPROFILE%\Desktop\Menu2.bat"
goto MainMenu
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
cls /?
echo /?
endlocal /?
exit /?
for /?
goto /?
rem /?
set /?
setlocal /?
title /?
For meaning of & in line endlocal & goto Menu%UsersChoice% see answer on Single line with multiple commands using Windows batch file.
I tried your code and what I found was that when the input was 1 both :1 and :2 are executed but when the input is 2 only :2 is executed. To fix this you need to specify the end of :1 using Exit or another goto.
You might see that none the batches are being executed IF you do not put a pause in the end of your script. They would be executed but the result might just flash out of the screen.
Also I do not understand why have you used \\Menu1.batand not \Menu1.bat in
:1
call %userprofile%\desktop\\Menu1.bat
The final working code for me-
#echo off
:MENU
title MENU0
Echo 1 - Select Menu 1
Echo 2 - Select Menu 2
Echo 0 - Exit
Echo.
SET /P choice=Type the number or letter of task you want, then press ENTER:
IF %choice%==1 GOTO 1
IF %choice%==2 GOTO 2
IF %choice%==0 EXIT
:1
call yourpathhere\Menu1.bat
pause
GOTO cont
:2
call whatsoever\Menu2.bat
pause
GOTO cont
:cont
exit
That should fix your problem.
Hope I helped.
I may not be a pro, but I could help you!
I always add extra code on my games in order to avoid bugs, like this:
set /p letter=
if %letter% == 1 goto nocheck1
if %letter% == 2 goto nocheck2
if %letter% == 3 exit
:nocheck1
if %letter% == 1 goto saves
:nocheck2
if %letter% == 2 goto howtoplay
Maybe it could work on your problem!
I might have the code to do it:
#echo off
cls
:menu
cls
echo 1. Open Batch 1
echo 2. Open Batch 2
set /p test=Enter number here ----->
if %test% == 1 goto check1
if %test% == 2 goto check2
Edit the "Batch file name" text with your location of your batch file.
:check1
if %test% == 1 start C:\Users\%username%\Desktop\(batch file name).bat
:check2
if %test% == 2 start C:\Users\%username%\Desktop\(batch file name).bat
If there's still any errors with my code, let me know.
Hope this helps your problem!
Use cd to go to the location of batch file. For example:
rem myscript
echo calling batch file
cd demo\desktop\script
execute.bat
echo done
After the execution of that batch, control will return to the next line of your script.
Use "Start" instead of "Call" like so,
#echo off
:MENU
title MENU0
Echo 1 - Select Menu 1
Echo 2 - Select Menu 2
Echo 0 - Exit
Echo.
SET /P choice=Type the number or letter of task you want, then press ENTER:
IF %choice%==1 GOTO 1
IF %choice%==2 GOTO 2
IF %choice%==0 EXIT
:1
start %userprofile%\desktop\\Menu1.bat
:2
start %userprofile%\desktop\Menu2.bat
Try This:
#echo off
:MENU
title MENU0
Echo 1 - Select Menu 1
Echo 2 - Select Menu 2
Echo 0 - Exit
Echo.
SET /P choice=Type the number or letter of task you want, then press
Enter:
IF %choice%==1 GOTO 1
IF %choice%==2 GOTO 2
IF %choice%==0 EXIT
:1
cd users
cd %userprofile%
cd desktop
:: call Menu1.bat or use: start Menu1.bat
:: exit
:2
cd users
cd %userprofile%
cd desktop
:: call Menu2.bat or use: start Menu2.bat
:: exit
start "" C:\location\of\file\file.bat
This opens a new window, and as long as you have more commands to follow, the previous file that is calling the new one will still run along with this one.
I have this batch code:
IF [%1]==[] GOTO NoParameter
IF NOT [%1]==[] GOTO Parameter
:NoParameter
SET /p "Name= ASM file name (exclude .ASM): "
TASM /ZI %Name%
IF ERRORLEVEL 1 GOTO End
TLINK /v %Name%
IF ERRORLEVEL 1 GOTO End
TD %Name%
GOTO End
:Parameter
TASM /ZI %1
IF ERRORLEVEL 1 GOTO End
TLINK /v %1
IF ERRORLEVEL 1 GOTO End
TD %1
:End
It works great in CMD, but not in DOSBox, where I actually need it. The script is not paused at SET /p, and proceeds to the next commands, with null as the parameter, then stops at TLINK because the given parameter is invalid. If I input a parameter when running the script from DOSBox, the script works perfectly. Any idea how I can get user input like this?
DOSBox doesn't support XP command extensions such as set /p, the developer was against it.
The only way to input a string of characters is to use another utility such as string25 mentioned on that forum.
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
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
I'm on Windows Server 2008 R2.
My script verifies that the files I enter exist and then verifies that the files I want to move don't already exist in recycleBin.dir. There's also a choice to overwrite the file or not; if you choose "yes," the script moves them.
My problem is I need to be able to input multiple files, not just one.
What I've done:
#echo off
set param = "%*"
set corb_path=c:\corbeille.dir
set rep_courant = %cd%
:debut
if "%*" == "" goto error
goto path
goto end
:path
cd %corb_path%|| del %corb_path%
if not exist %corb_path%/nul mkdir %corb_path%
cd c:\
if exist %rep_courant%%* goto something
) else (
goto end )
:something
if exist %corb_path%\"%*" goto choice
) else (
goto 1 )
:choice
choice /t 10 /d n /c on /cs /m "fichier "(%*)" file exist in corbeille.dir"
if errorlevel 2 goto 2
if errorlevel 1 goto 1
:1
move %* %corb_path%
shift
goto debut
:2
echo the file has beed deleted
goto end
:error
echo "You need to input something"
:end
You want your script to process each file passed in as command line parameters?
From what I see of the bat, you will need to change a few lines. The major issue is with using the %*. This will pull in all the parameters at once and not one at a time. You will want to use %1.
Change Line 6:
if "%1" == "" goto error
Change Line 13:
if exist %rep_courant%%1 goto something
Change Line 17:
if exist %corb_path%\"%1" goto choice
Change Line 21:
choice /t 10 /d n /c on /cs /m "fichier "(%1)" file exist in corbeille.dir"
Change Line 25:
move %1 %corb_path%
The %1 will always have the next item due to the shift command.
There are also some other changes in reagards to strings, quotations, and spaces that I might address later in an updated answer, but the changes above should do what you want.
BTW: Line 2 and 8 are doing nothing useful in the script. They can be removed.