Get user input to install file - batch-file

I'd like to be run this file and see
"1 for example_x
2 for example_y
3 for example_z
Enter number for apk to install:"
Then I press 1, 2, or 3 and enter and the script installs the corresponding .apk via adb (Android Debug Bridge).
When I run this now, I get the message "can't find '1' to install".
#echo off
set newline=^& echo.
set 1=example_x.apk
set 2=example_y.apk
set 3=example_z.apk
echo 1 for example_x %newline% 2 for example_y% 3 for example_z %newline%
set /p UserInput= Enter number for apk to install:
adb install %UserInput%
pause
exit

Ok, for your situation the choice command is what you are lookign for (use it in everycase possible). Here is what you need to do to your code:
(Also the problem is not with your command, it is when you use adb install %UserInput% look at the soloution)
Code :
#echo off
setlocal enabledelayedexpansion
set newline=^& echo.
set 1=example_x.apk
set 2=example_y.apk
set 3=example_z.apk
echo 1 for example_x %newline% 2 for example_y% 3 for example_z %newline%
choice /c 123 /n /m "Enter number for apk to install: "
set UserInput=%errorlevel%
adb install !%UserInput%!
pause
exit
Explanation of choice and !%UserInput%! :
Choice:
/n specify's not to display "[1,2,3}" (which you have already done)
/c Specify Opitions
/m "" Text to display
!%UserInput%!:
set UserInput=%errorlevel% where %errorlevel% is the ouput of the choice command
!%UserInput%! this only works if you setlocal enabledelayedexpansion and means, whatever result you get from %UserInput% is then treated as a variable itself. (! is treated the same as a %)
Hope this helped, to learn more, type the command followed by /?
Yours Mona

Related

How to prompt a user multiple times by a batch file with multiple user inputs?

I want to change the serial number and product number of about 250 servers (they are not correct at the moment). I know the command that I need to use, but I need to be able to input certain options in to that command and then run it.
The options I need are:
A prompt for selecting the update of the serial or the product number
Multiple prompts for actual serial number, an IP address, a user name and password on having selected before the update of the serial number
Multiple prompts for actual product number, an IP address, a user name and password on having selected before the update of the product number
The command I wish to run via the batch file is:
asu64 set SYSTEM_PROD_DATA.SysInfoSerialNum XXXXXXX --host XXX.XXX.XXX.XXX --user XXXX --password XXXX
The XXX is a user defined input. I'd also like for completion and then go back to the initial selection menu.
Here is what I have done so far. (Please excuse the simplicity of it. I am very new to this stuff.)
#ECHO OFF
ECHO Test Code
ECHO.
ECHO 1.Serial Number
ECHO 2.Product Name
ECHO.
CHOICE /C 12 /M "Enter your choice:"
ECHO.
IF CHOICE 1 GOTO SerialNumber
IF CHOICE 2 GOTO ProductName
:SerialNumber
ECHO Serial Number
GOTO End
:ProductNumber
ECHO Product Number
GOTO End
PAUSE
Many thanks for any help you can offer.
There could be used for this task:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "IpAddress="
set "NameUser="
set "Password="
:MainMenu
cls
echo/
echo 1 ... Serial number
echo 2 ... Product number
echo E ... Exit
echo/
%SystemRoot%\System32\choice.exe /C 12E /N /M "Enter your choice:"
if errorlevel 3 exit /B
if not errorlevel 1 goto MainMenu
echo/
if errorlevel 2 goto ProductNumber
set "NumberText=serial"
set "NumberOption=SYSTEM_PROD_DATA.SysInfoSerialNum"
goto NumberPrompt
:ProductNumber
set "NumberText=product"
set "NumberOption=SYSTEM_PROD_DATA.SysInfoProductNum"
:NumberPrompt
set "NumberValue="
set /P "NumberValue=Please enter %NumberText% number: " || goto NumberPrompt
set "NumberValue=%NumberValue:"=%"
if not defined NumberValue goto NumberPrompt
echo/
if defined IpAddress (set "Default= [%IpAddress%]") else set "Default="
:IpPrompt
set /P "IpAddress=Please enter IP address%Default%: "
if not defined IpAddress goto IpPrompt
set "IpAddress=%IpAddress:"=%"
if not defined IpAddress goto IpPrompt
echo/
if defined NameUser (set "Default= [%NameUser%]") else set "Default="
:NamePrompt
set /P "NameUser=Please enter user name%Default%: "
if not defined NameUser goto NamePrompt
set "NameUser=%NameUser:"=%"
if not defined NameUser goto NamePrompt
echo/
if defined Password (set "Default= [%Password%]") else set "Default="
:PasswordPrompt
set /P "Password=Please enter password%Default%: "
if not defined Password goto PasswordPrompt
set "Password=%Password:"=%"
if not defined Password goto PasswordPrompt
echo/
"%~dp0ASU64.exe" set %NumberOption% "%NumberValue%" --host "%IpAddress%" --user "%NameUser%" --password "%Password%"
echo/
if not errorlevel 1 (
echo The update of the %NumberText% number "%NumberValue%" was successful.
) else echo ERROR: The update of the %NumberText% number "%NumberValue%" failed!
echo/
%SystemRoot%\System32\choice.exe /C CE /N /T 10 /D C /M "Press C to continue or E to exit ..."
if not errorlevel 2 goto MainMenu
endlocal
The executable ASU64.exe is referenced with full path of the batch file which means the executable must be always in the same directory as the batch file. The current directory of cmd.exe processing the batch file does not matter in this case.
Please read the following answers for the explanation of the code:
How does the Windows Command Interpreter (CMD.EXE) parse scripts?
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
Single line with multiple commands using Windows batch file
DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/
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 /? ... explains %~dp0 ... drive and path of argument 0 which is the batch file path.
choice /?
echo /?
endlocal /?
exit /?
goto /?
if /?
set /?
setlocal /?

delete command gets run before for loop

I am trying to setup a batch script which can connect to a set of servers and execute start script. Since there is a password getting saved in the commands.txt file. I need to delete it after the execution of start on remote servers. But that del command gets executed before everything and which is causing issues for the loop and errors out that commands.txt file is missing. Not sure how thats getting executed before the loop when its put after the loop. How can I fix this?
Below is the code I am trying.
#echo off
Echo Please enter your password in the popup window and then press enter
set tempbat="%temp%\p.cmd"
REM Create temporary batch file to make popup window for entering password 'masked'
echo mode 20,1 >%tempbat%
echo color EF >>%tempbat%
echo Title Enter Password >>%tempbat%
echo setlocal enabledelayedexpansion >>%tempbat%
echo set /p Pass= >>%tempbat%
echo echo !pass!^>"%temp%\pass.txt" >>%tempbat%
echo exit >>%tempbat%
echo exit >>%tempbat%
start /wait "" %tempbat%
set /p Password=<"%temp%\pass.txt"
#echo echo %password% ^| sudo -S -u x0ats echo startup.sh start>>
%cd%\commands.txt
#echo read>> %cd%\commands.txt
for /f "delims=" %%a in (%cd%\serverlist.txt) DO (
Start PuTTY username#%%a -pw %password% -m "%cd%\commands.txt"
)
del %cd%\commands.txt}
There are a few issues in your CMD script, but most specifically, as I mentioned above you are not putting double quotes around paths.
Additionally you were adding spaces to the ends of all of your line sin the bat file but that will cause you to collect the wrong PW, you also didn't delete your temp bat file or password file after the fact.
It seems like a bit of trouble to go through just to pop up a separate window, you could forgo creating a second batch file and call a sub function instead by making your script support cmd line arguments.
In Any case this should work more as you expect:
#ECHO OFF
ECHO.Please enter your password in the popup window and then press enter
SET "_TempBat=%temp%\p.cmd"
SET "_PWFile=%temp%\pass.txt"
SET "_CMDs=%cd%commands.txt"
REM Create temporary batch file to make popup window for entering password 'masked'
ECHO.SETLOCAL>"%_TempBat%"
REM ECHO.ECHO OFF >"%_TempBat%"
ECHO.mode CON COLS=42 LINES=1 >"%_TempBat%"
ECHO.color CF>>"%_TempBat%"
ECHO.Title Enter Password >>"%_TempBat%"
ECHO.SET /p "_Pass=Enter Password: " >>"%_TempBat%"
ECHO.ECHO.%%_pass%%^>"%_PWFile%">>"%_TempBat%"
ECHO.exit>>"%_TempBat%"
ECHO.exit>>"%_TempBat%"
START /wait "" "%_TempBat%"
DEL /F /Q "%_TempBat%"
IF NOT EXIST "%_PWFile%" (
"%_PWFile%" Not Created!
) ELSE (
FOR /F "Tokens=*" %%A IN ('TYPE "%_PWFile%"') DO (
SET "_PW=%%A"
DEL /F /Q "%_PWFile%"
)
)
#ECHO.ECHO.%_PW% ^| sudo -S -u x0ats ECHO.startup.sh start>> "%_CMDs%"
#ECHO.read>> "%_CMDs%"
for /f "delims=" %%a in ('Type "%_CMDs%"') DO (
Start PuTTY username#%%a -pw %_PW% -m "%_CMDs%"
)
Pause
DEL /F /Q "%_CMDs%"
ALSO I changed the colors you chose Bright Yellow on Bright right is hardly readable, I used Bright white on Bright Red. oh and I also added an Echo Off into your second script because again that was not helpful to have all the extra stuff in there
Also I looked at this and noticed that you were not going to get the PW saved in the PW file so I fixed that by using %% so that the first % will get stripped off.

Call batch within batch?

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.

Batch Script - First "IF" selection does not work, rest do

I am having trouble with my code. If I run the file and follow the prompt, enter a workstation name then select site 1, it continues to open up the cmd window to execute the psexec command, it does not do the "IF NOT EXIST" for site 1, but for other sites it comes back fine. If no file exists it will output to the prompt, if it does it continues to psexec. Any idea what I am doing wrong? Thank you.
:MAN
SET /P S=Please enter a workstation name or IP:
ECHO.
ECHO 1 - Site 1
ECHO 2 - Site 2
ECHO 3 - Site 3
ECHO 4 - Site 4
SET /P D=Select which Site you want to deploy from:
IF %D%==1 SET D="\\site1\Operations\Sofware\Packages\file.msi"
IF %D%==2 SET D="\\site2\Operations\Sofware\Packages\file.msi"
IF %D%==3 SET D="\\site3\Operations\Sofware\Packages\file.msi"
IF %D%==4 SET D="\\site4\Operations\Sofware\Packages\file.msi"
IF NOT EXIST %D% GOTO MSG
START CMD /K PsExec.exe #%S% -s -h cmd /c msiexec.exe /i "%D%" /qn
PAUSE
GOTO EOF
:MSG
CALL :color 1a "ERROR: MSI PACKAGE DOES NOT EXIST"
Note the possibility of doing:
goto label%D%
You can also almost do:
set D=\\10.%D%.2.1\Operations\Sofware\Packages\quest.msi
instead of the 4 IF-statements.
Here is my working code, there was an issue with PsExec as well not taking the %s% variable, I instead outputted the user input to a text file and had Psexec read from that file. I also edited with everyone's suggestions. Thank you all.
:MAN
#ECHO OFF
SET /P S=Please enter a workstation name or IP:
ECHO %S% >> man.txt
ECHO.
ECHO 1 - Site 1
ECHO 2 - Site 2
ECHO 3 - Site 3
ECHO 4 - Site 4
SET /P D=Select which Branch you want to deploy from:
IF %D%==1 SET "R=\\site1\Operations\Sofware\Packages\file.msi"
IF %D%==2 SET "R=\\site2\Operations\Sofware\Packages\file.msi"
IF %D%==3 SET "R=\\site3\Operations\Sofware\Packages\file.msi"
IF %D%==4 SET "R=\\site4\Operations\Sofware\Packages\file.msi"
IF NOT EXIST %R% GOTO MSG
START CMD /k "PsExec.exe #man.txt -s -h cmd /c msiexec.exe /i %R% /qn & DEL man.txt"
PAUSE
GOTO MENU
:MSG
CALL :color 1a "ERROR: MSI PACKAGE DOES NOT EXIST"
== is not advised in a .bat, .cmd file; I'd use EQU

Batch file with choosing Y/N

Please help, I'm a beginner and I need to create batch-file to delete log-files. But before delete I need to ask user to delete the last log-file or all log-files.
I tried to implement it like:
set /P PARAM_VALUE=Remove only the last log file?(Y/N)?
if /I "%PARAM_VALUE%"=="y" goto DeleteLast
if /I "%PARAM_VALUE%"=="n" goto DeleteAll
But in this case I can enter any values, I need to avoid an others character except Y,y,N,n.
Thx.
One of solutionÑ– in this case is using the CHOICE command.
This tool allows users to select one item from a list of choices and returns the index of the selected choice.
Syntax: CHOICE [ /C choices ] [ /N ] [ /CS ] [ /T timeout /D choice ] [ /M text ]
Parameter List:
/C choices Specifies the list of choices to be created. Default list for English versions is YN
/N Hides the list of choices in the prompt. The message before the prompt is displayed and the choices are still enabled.
/CS Enables case-sensitive choices to be selected. By default, the utility is case-insensitive. Note: DOS and NT Resource Kit versions use /S instead
/T timeout The number of seconds to pause before a default choice
is made. Acceptable values are from 0 to 9999.
If 0 is specified, there will be no pause and the default choice is selected.
Note: DOS and NT Resource Kit versions use /T:default,timeout instead.
/D default Specifies the default choice after timeout seconds.
Character must be in the set of choices specified by /C option and must also specify timeout with /T.
Note: DOS and NT Resource Kit versions use /T:default,timeout instead.
/M text Specifies the message to be displayed before the prompt.
If not specified, the utility displays only a prompt.
For your problem try to use something like:
choice /C:YN /M:"Remove only the last log file? [YN]"
IF ERRORLEVEL ==1 GOTO DeleteLast
IF ERRORLEVEL ==2 GOTO DeleteAll
GOTO next
:DeleteAll
rem delete all
:DeleteLast
rem delete last
:next
rem continue work with Batch file
You can just catch all other inputs like this:
:CONFIRM
if /i "%PARAMVALUE%"=="y" goto DeleteLast
if /i "%PARAMVALUE%"=="n" goto DeleteAll
echo Please enter only Y/y or N/n!
ping localhost -n 2 >nul
goto CONFIRM
I used this too for my own scripts and it works wonderfully and is super easy!
The delete command already offers a prompt N/y
EDIT
del /p
type del /? in console for more information
UPDATE
you can test with this example:
#echo off
SETLOCAL
Echo:
Echo /!\ Creating a bunch of empty files /!\
echo Press any key to continue or CTRL-C to exit...
Pause>nul
for /L %%A in (1,1,4) do (copy /y NUL _temp_file_%%A.ext)
Echo:
Echo /!\ Deleting a bunch of empty files with command prompt /!\
echo Press any key to continue or CTRL-C to exit...
Pause>nul
del /p "_temp_file_?.ext"
Echo:
Pause>nul|(echo Press any key to exit...)

Resources