What can I do if I want to echo a warning if the variable favnum is not filled?
I already tried this:
echo What's your favorite color?
echo.
set /p favnum=">"
if not defined favnum(
goto nope
)
give it a space between favnum and (
As you wrote it, if checks for a variable named favnum(. It then misses a command to execute and spits a syntax error.
As pointed out previously you missed out a space between favnum and (
You could also use
echo What's your favorite color?
echo.
set /p favnum=">"
if "%favnum%"=="" (
goto nope
)
or put is the variable not set on one line
echo What's your favorite color?
echo.
set /p favnum=">"
if "%favnum%"=="" goto nope
which would make your example
echo What's your favorite color?
echo.
set /p favnum=">"
if not defined favnum goto nope
Related
I'm usually stuck on making a game now...I can't solve the the call command problem This is an example :::
:verifile1
cls
echo.
echo Before you can continue give out the following information...
echo.
echo What is your username?
echo.
set /p name1=Username:
if not exist "%name1%_1.bat" (
echo Invalid Username
pause>nul
goto welcome
)
echo.
echo Your password?
echo.
set /p pass1=Password:
call label %name1%_1.bat
if not %password1% EQU %pass1% (
echo Password entered do not match
pause>nul
goto welcome
)
goto Story
Please help me with this case
if you are calling a label then call it thus
Call :Label arg
call label %name1%_1.bat
calls an utility named label (yes, there happens to be one...) and gives it %name1%_1.bat as parameter. Probably not quite what you want.
To call a label inside your batchfile, use:
call :label
But I guess, you simply want to call your second batchfile %name1%_1.bat. To do so, simply:
call %name1%_1.bat
so I'm writing batch and come up with the ( was unexpected error after setting the first variable. I want a batch file to start with just setting the color. (I named it edmond)
this is my code
#echo off
SETLOCAL
title Edmond
goto :Check
:Prompt
set /p action=What shall I do for you master?
:Check
if not defined action goto :Prompt
echo %action%
pause
if /i %%action == color (
echo stuff
:cl
set /p BC=What Should the background color be?
set /p FC=and the foreground color?
if %BC%==Black set BC1=0
if %BC%==Blue set BC1=1
if %BC%==Green set BC1=2
if %BC%==Aqua set BC1=3
if %BC%==Red set BC1=4
if %BC%==Purple set BC1=5
if %BC%==Yellow set BC1=6
if %BC%==White set BC1=7
if %BC%==Gray set BC1=8
if %BC%==LBlue set BC1=9
if %BC%==LGreen set BC1=a
if %BC%==LAqua set BC1=b
if %BC%==LRed set BC1=c
if %BC%==LPurple set BC1=d
if %BC%==LYellow set BC1=e
if %BC%==LWhite (
set BC1=f
) else (
echo I'm sorry, I didn't exactly understand that.
echo By any chance could you say it again?
goto cl
)
if %FC%==Black set FC1=0
if %FC%==Blue set FC1=1
if %FC%==Green set FC1=2
if %FC%==Aqua set FC1=3
if %FC%==Red set FC1=4
if %FC%==Purple set FC1=5
if %FC%==Yellow set FC1=6
if %FC%==White set FC1=7
if %FC%==Gray set FC1=8
if %FC%==LBlue set FC1=9
if %FC%==LGreen set FC1=a
if %FC%==LAqua set FC1=b
if %FC%==LRed set FC1=c
if %FC%==LPurple set FC1=d
if %FC%==LYellow set FC1=e
if %FC%==LWhite (
set FC1=f
) else (
echo I'm sorry, I didn't exactly understand that.
)
echo Applying changes.
ping localhost -n 2 >nul
color %FC%%BC%
) else (
echo I'm sorry, I didn't exactly understand that.
)
pause
exit
after the set /p action=what shall I do for you master? it says ( was unexpected and then shuts down (I got the ( was unexpected by printing screen before it closes) can someone help? thanks (in advance)
if /i %%action == color (
should be
if /i %action% == color (
or better
if /i "%action%"=="color" (
since you have uncontrolled input that may contain spaces or other separators.
Your upcoming problems are many-fold.
You can't use a label in a (block statement - a series of parenthesised statements)
You need to
setlocal enabledelayedexpansion
then use !var! in place of %var% withing a block statement to access the value of any ordinary environment variable changed or established within the block.
Very much truncated revised structure:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:Prompt
set /p action=What shall I do for you master?
:Check
if not defined action goto :Prompt
echo %action%
if /i "%action%"=="color" GOTO setcolor
echo I'm sorry, I didn't exactly understand that.
goto Prompt
:setcolor
echo stuff
:cl
set /p BC=What Should the background color be?
set /p FC=and the foreground color?
:: This forces BC1 & FC1 to be undefined
SET "BC1="
SET "FC1="
if /i "%BC%"=="Black" set BC1=0
if /i "%BC%"=="Blue" set BC1=1
:: Your job to fill in the rest
IF NOT DEFINED BC1 (
echo I'm sorry, I didn't exactly understand that.
echo By any chance could you say it again?
goto cl
)
if /i "%FC%"=="Yellow" set FC1=6
:: Your job to fill in the rest
IF NOT DEFINED BC1 (
echo I'm sorry, I didn't exactly understand that.
rem I suppose you really want to re-enter at this point.
rem note that you need to use REM within a block, not ::-style comments.
goto cl
)
echo Applying changes.
ping localhost -n 2 >NUL
:: Best to use FC1 and BC1 here, else you'll try to execute "color YellowBlack"
:: And it's likely you have them reversed.
color %FC1%%BC1%
GOTO :EOF
I have a script of batch (modeled after Joshua in "War Games")
#echo off
color 0b
echo Greetings, Professor Falken
set /p interface =
echo Would You Like to Play a Game?
set /p ifGame =
if /i "%ifGame%" =="yes" goto yesgame
if /i "%ifGame%" =="no" goto nogame
:yesgame
echo List of Games
echo chess
echo Poker
echo Fighter Combat
echo Guerilla Warfare
echo Desert Warfare
echo Air-to-Ground Actions
echo Theaterwide Tactical Warfare
echo Theaterwide Biotoxic and Chemical Warfare
echo Global Thermonuclear War
pause
echo Which game would you like to play?
set /p WhichGame =
pause
exit
:nogame
set /p areYouSure=Are You Sure?
pause
exit
But, when I enter "No" it still shows the list of games...
You need to remove the spaces between the variable name and the = symbol when using set /p. From:
set /p ifGame =
To:
set /p ifGame=
Otherwise the variable you set has a space at the end of the name. So %ifGame% expands to nothing, whereas %ifGame % will expand to the correct value.
What I'm looking for is a batch file line of code that will scan what the user inputs to find key words and direct them in the right place. That way when a trainee has a question he/she could just ask the batch file and it will direct them to the proper menu. Is this possible? if so, How would one go about doing this?
:menu
set /p c=Please type you question:
findstr /m "How to ringout a product on our system?" "%c%"
if %c%=="ringout" (
goto :Ringingout
) Else (
goto :Sorry
)
:Ringingout
cls
echo In order to right something out maksure you do the following:
echo - Log in
echo - click on scan in the bottom left had corner on the tender page
echo - scan items
echo - click continue
Pause
goto :Menu
:Sorry
cls
echo Sorry I don't recognize your question, please re-word it.
pause
goto :Menu
#ECHO OFF
SETLOCAL
SET "keywords=word anotherword someword"
FOR %%k IN (%keywords%) DO set "#%%k="
SET /p query="Please enter your query ? "
FOR %%k IN (%keywords%) DO CALL :analyse %%k
SET #
GOTO :EOF
:analyse
CALL SET "found=%%query:%1=%%"
IF "%found%"=="%query%" GOTO :EOF
SET #%1=FOUND!
GOTO :eof
Here's a general way to do it.
If one of the keywords is entered, the variable #keyword will be set to FOUND! so you can use if defined #keyword to process from there.
It's not protected against destructive user-inputs - that's not what this question is about...
You'd be better off collecting all the questions in a document (like .html) and letting the user search that document for what they need. But if this is just an exercise, you can re-write your logic like so to make your program work:
:menu
set "c="
set /p "c=Please type your question: "
echo %c% | findstr /i /b "ringout" >nul
if errorlevel 1 goto Sorry else goto Ringingout
:Ringingout
and so on.
I need to load different numeric values from external config.txt file and write them to %variables% in batch file. Example - config.txt file should looks as following:
====================
Setting1=1
Setting2=0
Setting3=1
====================
I need to assign first value (1) lets say into variable %1%, second value (0) into variable %2% and so on.
Could you please help me how to do this?
Thanks.
try this:
#echo off&setlocal
for /f %%i in (config.txt) do set "%%i" 2>nul
set "setting"
Do you know how many variables there will be? If you don't then go with Endoro's answer. If you do know how many variables there will be and you want to set them to a custom name you could do this:
#echo off
< config.txt (
set /p var1=
set /p var2=
set /p var3=
)
echo %var1%
echo %var2%
echo %var3%
pause
You shouldn't use %1% or plain numbers for variables, it can mess it up.
If config.txt had those '=' signs then you would have to skip add two more lines for the var's.
The reason you should use this for custom variables is because you could name the variables by there specific meaning which may make it easier to remember when coding.
Ex.
#echo off
< config.txt (
set /p name=
set /p pizza=
set /p car=
)
echo %name%
echo %pizza%
echo %car%
pause