Batch file using If statement and keyboard input
batch-file, if-statement, user-input
Solution
Here's some code to get you in the right direction. I tested this with a .cmd file in Windows 7.
ECHO OFF
SET /p environment="Connect to Live or Dev?"
IF /i "%environment%" == "Live" GOTO live
IF /i "%environment%" == "Dev" GOTO dev
ECHO Invalid Option
GOTO end
:live
ECHO "Live!!!!"
goto end
:dev
ECHO "Dev!!!"
:end
PAUSE
Problem
A slightly late Happy New Year to everyone, hope you had a good one. I'm trying to create a batch file that will take keyboard input and then either launch a program based on that input or display an error message if the option entered isn't valid. But it's not working. Here's what I've got so far: ``` ECHO OFF set /p %environment%=Connect to Live or Dev?: IF %environment% = "Live" ( C:\Windows\System32\runas.exe /user:live\someuser /netonly "C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\Ssms.exe" ) ELSE IF %environment% = "Dev" ( C:\Windows\System32\runas.exe /user:dev\someuser /netonly "C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\Ssms.exe" ) ELSE ( ECHO "Invalid Option" ) ``` I've tried it with and without the % round the variable but it didn't work. Can anyone point me in the right direction? Hope you can help. Cheers Alex