Get user input without pressing enter - windows

Here is my code, I don't want the user to have to press enter to make a choice. I tried a couple of things but nothing I tried worked.
#echo off
:debut mon menu a moi
cls
echo 1.Calculatrice
echo 2.Task manager
echo 3.msconfig
echo 4.Notepad
echo 5.ipconfig
echo 6.quit
echo.
set /p /c choix=" Choisir votre application : "
if "%choix%"=="1" goto menu1
if "%choix%"=="2" goto menu2
if "%choix%"=="3" goto menu3
if "%choix%"=="4" goto menu4
if "%choix%"=="5" goto menu5
if "%choix%"=="6" goto menu6
I didn't include the menu since it's not relevant

#echo off
cls
echo 1.Calculatrice
echo 2.Task manager
echo 3.msconfig
echo 4.Notepad
echo 5.ipconfig
echo 6.quit
echo.
choice /C 123456 /M "Choisir votre application : "
goto menu%errorlevel%
:menu1
echo Calculatrice
. . .
:menu6
:menu0
echo Quit
The choice command returns an errorlevel value between 1 and 6 (or 0 if the user press Ctrl-C), so goto menu%errorlevel% command direclty transfer to the desired label without need of any additional checking. For further details, type: choice /?

#echo off
setlocal enableextensions disabledelayedexpansion
rem OPTION 1 - Use choice command (if available)
echo ---------------------------------------------------
rem Ask for the options
choice /n /c 1234560 /m "Choisir votre application : "
rem Each option sets a errorlevel in the same order they are included.
rem It is necessary to test in decreasing order
if errorlevel 7 (
echo selected 0
) else if errorlevel 6 (
echo selected 6
) else if errorlevel 5 (
echo selected 5
) else if errorlevel 4 (
echo selected 4
) else if errorlevel 3 (
echo selected 3
) else if errorlevel 2 (
echo selected 2
) else if errorlevel 1 (
echo selected 1
)
rem Alternatively, this sintax can be used
rem In this case the order in the tests is not relevant
if "%errorlevel%"=="1" echo selected 1
if "%errorlevel%"=="2" echo selected 2
if "%errorlevel%"=="3" echo selected 3
if "%errorlevel%"=="4" echo selected 4
if "%errorlevel%"=="5" echo selected 5
if "%errorlevel%"=="6" echo selected 6
if "%errorlevel%"=="7" echo selected 0
rem OPTION 2 - Use xcopy to retrieve the key press
echo ---------------------------------------------------
rem Show the prompt
<nul set /p ".=Choisir votre application : "
rem Retrieve the user input
set "choix=" & for /f "delims=" %%a in ('xcopy /l /w "%~f0" "%~f0" 2^>nul') do if not defined choix set "choix=%%a"
set "choix=%choix:~-1%"
rem Echo user input
echo %choix%

Related

If statement in batch file - user chooses option

New to batch/sql files. Thanks for you help.
What im trying to do:
user picks option 1, runs my sql file.
user picks 2, exits program.
user enters nothing or invalid option, ECHO "invalid option".
Problem: anything I type will exit my program. What am I doing wrong?
ECHO 1 - Show Report
ECHO 2 - Exit
SET choice=""
SET /P input=Your choice:
IF "%input%"=="1" (
GOTO :sql file
) ELSE (
IF "%input%"=="2" (
GOTO :Exit
)
) ELSE (
IF "%input%"=="" (
ECHO Invalid option chosen.
)
)
PAUSE
Just use the choice command, its instructions are available by entering choice /? at the Command Prompt.
#ECHO OFF
ECHO 1 - Show Report
ECHO 2 - Exit
CHOICE /C 12 /M "Your choice"
IF ERRORLEVEL 2 GOTO :EOF
REM This line is your SQL report code
PAUSE
If you still wanted to use, what in your case I believe is the wrong Set /P input method, then something like this should be relatively robust, given that your end user is free to enter whatever they like as input, mischievous or harmful intent included:
#ECHO OFF
ECHO 1 - Show Report
ECHO 2 - Exit
:GETInput
SET "input="
SET /P "input=Your choice: "
2>&1 SET input | FINDSTR /XR "input=[12]" 1>NUL || (
ECHO Invalid option chosen
GOTO GETInput
)
IF %input% EQU 2 GOTO :EOF
REM This line is your SQL report code
PAUSE
There is one extract closing bracket [Line #12] inside nested if..else. Also, as far as i know, your label :sql file should not contain space and you must declare it inside the batch file (Same for label :Exit)
I did few changes in your code to make it work
#echo off
:Main
cls
ECHO 1 - Show Report
ECHO 2 - Exit
SET choice=""
SET /P input=Your choice:
IF "%input%"=="1" (
GOTO :SQLReport
) ELSE IF "%input%"=="2" (
GOTO :EOF
) ELSE (
ECHO.
ECHO Invalid option chosen.
pause
GOTO :Main
)
:SQLReport
ECHO.
ECHO Running SQL Report...
GOTO :EOF
:EOF

Unable to load a .cmd from a batch file

I recently played the game "Dictator" and wanted to recreate it in a batch script. I just began creating it when I ran into an issue. I have made programs which allow the user to save their progress and load it. I copied this code from a different program and put it in mine. Saving does seem to work but when i edit the save file (.cmd) and edit one of the saved variables, once I load that file nothing seems to happen. For example I saved one file called key.cmd and edited it to make the "pol" variable equivalent to 100. Once I loaded that file it did say loaded but the pol variable was still set to the default (10). I dont understand as I have made a folder called save and key.cmd file is inside it. I have used my save and load code multiple times in the past and have never had any issues. Please help! Here is my code:
:Setup
#echo off
title Dictator
color 0a
:Save Variables
set pol=10
set bui=10
set low=10
set cor=10
set peo=10
set cri=10
:Main
cls
echo 1 - Save
echo 2 - Load
echo 3 - Police - %pol%
echo 4 - Buissnes Men - %bui%
echo 5 - Lower Government - %low%
echo 6 - Corruption - %cor%
echo 7 - People - %peo%
echo 8 - Criminals - %cri%
choice /c 12345678 /n /m ">>> "
if %errorlevel% equ 1 goto Save
if %errorlevel% equ 2 goto load
:Save
cls
set /p pin="PIN: "
(
echo set pol=%pol%
echo set bui=%bui%
echo set low=%low%
echo set cor=%cor%
echo set peo=%peo%
echo set cri=%cri%
) >> saves\%pin%.cmd
echo SAVED
pause >nul
goto main
:Load
cls
set /p pin="PIN: "
if exist saves\%pin%.cmd (
call saves\%pin%.cmd
echo %pol%
echo LOADED
pause >nul
) else (
echo INCORRECT PIN
pause >nul
)
goto main
At :Load you need to allow spaces in the file name. Most users won't know of this weakness, but in order to allow spaces (and a few other 'special' characters) to be read in the user input without breaking it, you need to add setlocal enableextensions enabledelayedexpansion to the beginning of your batch file and make the set /p pin="PIN: " statement into set /p "pin=PIN: " and replace the %'s with !'s where it calls for %pin%.
In the end, your code should look like:
:Setup
setlocal enableextensions enabledelayedexpansion
#echo off
title Dictator
color 0a
:Save Variables
set pol=10
set bui=10
set low=10
set cor=10
set peo=10
set cri=10
:Main
cls
echo 1 - Save
echo 2 - Load
echo 3 - Police - %pol%
echo 4 - Buissnes Men - %bui%
echo 5 - Lower Government - %low%
echo 6 - Corruption - %cor%
echo 7 - People - %peo%
echo 8 - Criminals - %cri%
choice /c 12345678 /n /m ">>> "
if %errorlevel% equ 1 goto Save
if %errorlevel% equ 2 goto load
:Save
cls
set /p "pin=PIN: "
(
echo set pol=%pol%
echo set bui=%bui%
echo set low=%low%
echo set cor=%cor%
echo set peo=%peo%
echo set cri=%cri%
) >> saves\!pin!.cmd
echo SAVED
pause >nul
goto main
:Load
cls
set /p "pin=PIN: "
if exist saves\!pin!.cmd (
call saves\!pin!.cmd
echo %pol%
echo LOADED
pause >nul
) else (
echo INCORRECT PIN
pause >nul
)
goto main

Trying to make a telefphonebook in batchscript Win8

I'm a beginner to batchscripting and I'm trying to make a telephoneregister that prints all, adds, deletes and searches for phonenumbers, but I can't get it to work correctly and I'm wondering where I did go wrong. The code is down below, thanks in advance.
echo Print out all content ^<1^>
echo Add a new number ^<2^>
echo Delete a number ^<3^>
echo Search ^<4^>
echo Exit ^<5^>
set /p val="Choose between 1-5: "
GOTO CASE_%val%
:CASE_1 for /f "tokens=*" %%a in
(telephoneregister.txt) do
( echo %%a )
GOTO END_SWITCH
:CASE_2 echo "Number: " set /p p1="Nr"
echo %p1% >> %output%\telephoneregister.txt
GOTO END_SWITCH
:CASE_3 echo "Which number would you like to delete? "
set /p num="Telephoneregister"
type telephoneregister.txt | findstr /v %num% >telephoneregister.txt del /s telephoneregister.txt
type telephoneregister.txt > tele.txt del /s tele1.txt
GOTO END_SWITCH
:CASE_4 set /p n1="Number: "
findstr %n1% telephoneregister.txt
GOTO END_SWITCH
:CASE_5 exit 0
GOTO END_SWITCH
:END_SWITCH
pause
I got to this code here:
#echo off
:menu
cls
echo 1- Print out all content
echo 2- Add a new number
echo 3- Delete a number
echo 4- Search
echo 5- Exit
set /p val="Choose between 1-5: "
if %val%== 1 goto one
if %val%== 2 goto two
if %val%==3 goto three
if %val%== 4 goto four
if %val%== 5 goto five
:one
cls
type telephoneregister.txt
echo.
echo ====================
set /p =ENTER to go back to menu.
goto menu
:two
cls
set /p p1="Number: "
echo %p1% >> telephoneregister.txt
echo.
echo ====================
set /p =ENTER to go back to menu.
goto menu
:three
cls
echo Which number would you like to delete?
set /p num="Telephoneregister: "
type telephoneregister.txt | findstr /v %num% >telephoneregister.txt del /s telephoneregister.txt
type telephoneregister.txt > tele.txt del /s tele1.txt
set /p =ENTER to go back to menu.
goto menu
:four
cls
set /p n1="Number: "
findstr /r /c:%n1% telephoneregister.txt
echo.
echo ====================
SET /p =ENTER to go back to menu.
goto menu
:five
exit
It's not completely done yet (Option 3 It's not working) but you can see what you had wrong, and probably come to the solution searching on the web. If you have any question in the code above, Just ask! Good luck.

Inputting space in prompts crash batch file

I am trying to do a command line replica compeletely out of boredom and I have an issue. Here is my code:
#echo off
set /a secret=1
color B
title Subnet Access v1.0
echo ____________________________________________________________
echo / I
echo I Subnet Access v1.0 I
echo I____________________________________________________________/
echo.
echo.
timeout 3 > NUL
echo sys rev init
timeout 1 >NUL
echo loading keyframes
echo please wait
timeout 5 >NUL
echo.
echo.
echo Checking for alerts
timeout 6 >NUL
echo alert 0-21-77
timeout 1 >NUL
echo.
set /p pid=enter pid: || set pid=empty
IF "%pid%"=="1123321231" (
echo.
set /a "secret+=1"
cls
echo Secrets %secret%/10 found.
timeout 3 >NUL
cls
echo.
echo pid confirmed
timeout 1 >NUL
echo checking with server...
timeout 5 >NUL
cls
color C
echo COULD NOT IDENTIFY
timeout 1 >NUL
cls
echo.
timeout 1 >NUL
cls
echo COULD NOT IDENTIFY
timeout 1 >NUL
cls
echo.
timeout 1 >NUL
cls
echo COULD NOT IDENTIFY
timeout 1 >NUL
cls
echo.
timeout 1 >NUL
goto :unidentify
)
IF NOT "%pid%"=="1123321231" GOTO :unidentify
:unidentify
cls
echo.
echo unauthorized access
timeout 3 >NUL
echo level drop
timeout 1 >NUL
echo dropping...
timeout 3 >NUL
echo level now 0
timeout 4 >NUL
echo sys exit
timeout 1 >NUL
cls
cls
set su=0
color 7
title sys/rev/console-override
echo Subnet Console v0.1
echo Made by Murtaugh
echo.
:sub01 ::PROBLEM STARTS HERE
IF %su%==0 (
set /p commandMur=sys/dev/: #^> || set commandMur=emptycom
)
IF %su%==1 (
color B
set /p commandMur=sys/dev/: $^> || set commandMur=emptycom
)
IF %commandMur%==help (
::help segment
echo.
echo Help v0.1 Alpha by Murtaugh
echo.
echo Commands are listed below:
echo dir - Lists your directory's contents.
echo help - Shows this page.
echo connect - Connects you to an IP address.
echo.
goto :sub01
)
IF "%commandMur%"=="exit" (
goto :eof
)
IF "%commandMur%"=="cls" (
cls
goto :sub01
)
IF "%commandMur%"=="su" IF "%su%"=="0" (
cls
echo.
set /a "secret+=1"
echo Secrets %secret%/10 found.
set su=1
goto :sub01
)
IF "%commandMur%"=="su" IF "%su%"=="1" (
goto :sub01
)
IF "%commandMur%"=="emptycom" (
goto :sub01
)
IF NOT "%commandMur%"=="exit" IF NOT "%commandMur%"=="help" IF NOT "%commandMur%"=="cls" IF NOT "%commandMur%"=="su" (
echo "%commandMur%": Bad command or file name
echo.
goto :sub01
) ::PROBLEM ENDS HERE
But when I input something with spaces it says test==exit was unexpected at this time. and just close. Is there anyway I can fix this? Thanks.
EDIT: Tried "%prompt%"=="test" thing, doesn't work.
test==exit was unexpected ... is a typical syntax error with if (set doesn't give this type of error).
Most likely you have a if %command%==exit ...-command the next line. To avoid the syntax error, write if "%command%"=="exit" ...
im not really a great programmer, but try adding "QUOTATION TAGS" when you type text with spaces, beacuse batch sometimes get confused about those spaces

multiple windows to 1 single window batch

I have 2 batch files I want to merge from 2 screens to 1 screen.
currently there is 2 screens:
1 screen is the chat room
1 screen is the message input screen.
I want to merge these into 1 batch file on the screen so it requests users input on the chat room screen
is that possible?
My code I want to merge is here:
----------------------------
chat.bat
----------------------------
#echo off
:StartUp
set rr=2
mode con cols=40 lines=8
if exist tmp.bat del /f /s /q tmp.bat
set hd=%cd%
title Remote Chat
color 0f
cls
echo [Remote Chat]
echo.
echo Made by Jordan Begg
echo 31/12/2013
set t1=%time:~6,2%
set /a t2=%t1%+2
if %t2% geq 60 set /a t2=%t2% - 59
:ExpLoop
if %time:~6,2% geq %t2% goto Go
goto ExpLoop
:Go
set cs=ChatSettings.bat
if exist %cs% (
call %cs%
goto Main
) else (
goto SetUp
)
:SetUp
cls
mkdir "C:\chat"
echo [Setup]
echo.
set /p un=Username:
if not defined un goto SetUp
:SetUp2
cls
echo [Setup]
echo.
echo Use this:
echo C:\chat
if defined sd echo Current: %sd%
echo.
set /p sd=Server folder:
if not defined sd goto SetUp2
set sd=%sd%
if not exist %sd% (
set sd=
echo Folder doesn't exist!
pause >nul
goto SetUp2
)
if exist %cs% del /f /s /q %cs%
echo set un=%un%>>%cs%
echo set sd=%sd%>>%cs%
echo set rr=4>>%cs%
goto Main
:Main
cd %hd%
cls
echo [Remote Chat]
echo.
echo 1 - Join
echo 2 - Create
echo 4 - Exit
echo.
set /p c=
if %c%==1 goto Join
if %c%==2 goto create1
if %c%==4 goto Exit
goto main
:Exit
exit
:create1
cls
echo Please enter a valid asset ID (i.e as0123456)
set /p ass=Asset:
ping -n 1 %ass% > c:\ping.tmp
type c:\ping.tmp | FIND "Approximate round trip" >NUL
IF ERRORLEVEL 1 GOTO NOPING
goto Create
:noping
echo Asset cannot be reached
echo.
goto create1
:Create
cd %sd%
set rn=
cls
echo [Create]
echo.
echo created
echo.
set rn=%ass%
echo %rn%
pause
if not defined rn goto Create
set rf=%sd%\%rn%.crm
if exist %rf% goto Create
cls
echo [Create]
echo.
echo Preparing room, please wait.
set group=Admin
echo [%time:~0,5%] %un% (admin) connected.>>%rf%
title Chat [%rn%]
cd %hd%
echo set rn=%rn%>>tmp.bat
echo set rf=%rf%>>tmp.bat
echo set group=%group%>>tmp.bat
start ChatMessage.bat
start ChatDisconnect.bat
cd %sd%
mode con cols=40 lines=16
:Admin
cls
cd %hd% >nul
if not exist tmp.bat (
set msg=Room closed successfully.
goto End
)
cd %sd% >nul
if not exist %rf% (
set msg=Connection terminated.
goto End
)
type %rf%
ping localhost -n 4 >nul
goto Admin
set t=%time:~7,1%+%rr%
if %t% GEQ 10 set /a t=%t% - 10
:AdminL
if %time:~7,1%==%t% goto Admin
:End
if exist tmp.bat del /f /s /q tmp.bat
mode con cols=40 lines=8
echo [Chat]
echo.
echo %msg%
set msg=
taskkill /f /fi "WINDOWTITLE eq Message" >nul
taskkill /f /fi "WINDOWTITLE eq Disconnect" >nul
title Chat
set group=
set rn=
set t1=%time:~6,2%
set /a t2=%t1%+2
if %t2% geq 60 set /a t2=%t2% - 5
:EndLoop
if %time:~6,2% geq %t2% goto Main
goto EndLoop
:Join
mode con cols=40 lines=16
cd %sd%
set rn=
cls
echo [Join]
echo.
if not exist %sd%\*crm (
echo No rooms available.
pause >nul
mode con cols=40 lines=8
goto Main
)
dir /b /o:n %sd%\*.crm
echo.
echo Format (no .crm at end): Room
set /p rn=Room's name:
if not defined rn goto Join
set rf=%sd%\%rn%.crm
if not exist %rf% goto Join
cls
echo [Join]
echo.
echo Joining room, please wait.
set group=User
echo [%time:~0,5%] %un% connected.>>%rf%
title Chat [%rn%]
cd %hd%
echo set rn=%rn%>>tmp.bat
echo set rf=%rf%>>tmp.bat
echo set group=%group%>>tmp.bat
start ChatMessage.bat
start ChatDisconnect.bat
cd %sd%
:User
cls
cd %hd% >nul
if not exist tmp.bat (
set msg=Disconnected successfully.
goto End
)
cd %sd% >nul
if not exist %rf% (
set msg=Connection terminated.
goto End
)
type %rf%
ping localhost -n 4 >nul
goto User
.
-----------------------
chatmessage.bat
------------------------
#echo off
if not exist tmp.bat (
exit
) else (
call tmp.bat
call ChatSettings.bat
)
title Message
mode con cols=30 lines=2
cd %sd%
:func
cls
set /p msg=Message:
if %msg:~0,3%==/me echo [%time:~0,5%] %un% %msg:~4%>>%rf%&& goto func
if not defined msg goto func
echo [%time:~0,5%] %un%: %msg%>>%rf%
goto func

Resources