I can't use spaces with the if statement - batch-file

I'm trying to make a program like clever bot but when the uses spaces it won't work. Please help.(:
#echo off
set text=Hello
:start
cls
echo %text%
set /p input=:
if %input%==a b set text=It worked
goto start

Use quotes:
#echo off
set text=Hello
:start
cls
echo %text%
set /p input=:
if "%input%"=="a b" set text=It worked
goto start
It will also handle null input.
Mona

Related

Test for and remove substring in user input

I want to create a batch file that echoes text that it receives as part of a user-inputted command.
#echo off
Echo execute a command
Set /p command=
if "%command%"=="echo 'some text'" goto echo
::my aim is to make "echo" ignored by System and Only read "'some text'"
:: like "set text_to_echo='some text'
:echo
echo %text_to_echo%
Asking the user for the command first and then asking the user what to echo, like below, is not an option
#echo /p
set /p text=
if "%text%=="echo"
:echo
set /p tex1="Enter text to echo"
echo "%tex1%"
You could use this:
#echo off
Echo execute a command
Set /p "command="
if NOT "%command%"=="%command:echo =%" goto echo
pause
:echo
SET "text_to_echo=%command:~5%"
echo.%text_to_echo%
pause
Note that I also fixed your if, and put a pause after your if so you know whether it goes to :echo or not.
You want to split your Input into a first word (token) and the rest. You can do this with a for:
#echo off
Echo execute a command
Set /p "commandstring=# "
for /f "tokens=1,* delims= " %%a in ("%commandstring%") do (
set "command=%%a"
set "params=%%b"
)
if /i "%command%"=="echo" goto :_echo
...
:_echo
echo %params%
REM also possible (at least for "echo", but I guess, that's just an example):
%command% %params%

Batch Calculator not working

I'm new to batch and am practicing by making a calculator. I've made it but its not working. So I want to check with more experienced people to see whats wrong.
#echo off
set MATH=Equation:
set /a result=%MATH%
echo %result
You need to do set /p instead of just set (set /p means user input)
if you were to do that it would look like this
#echo off
title CALCULATOR
:start
cls
set /p MATH=Equation:
set /a result=%MATH%
echo %result%
pause
goto :start

How to use set and if statements in batch files

So i'm trying to make a small batch program with some sets and ifs, seems easy enough right? Apparently, you can't use "set" and "if" like this.
#echo off
setlocal enabledelayedexpansion
cls
set variableTrue EQU 1
Then continue the code and later in the program do this.
If %variableTrue% EQU 1 goto next
Please note I've tried it with exclamation marks as well.
With the exclamation marks, it's like it completely ignores the statement, even if it's true it will continue as usual. With the percentage signs, it says very quickly before crashing
"1" was not expected at this time.
Or something like that, like I said it barely stayed for half a second.
I always thought you could do it like this as long as there are no conflicting variables.
:start
#echo off
setlocal enabledelayedexpansion
title test
color a
cls
:favnum
cls
echo What is your favorite number?
set /p fn=Favorite Number
If "!fn!" NEQ 13 goto thanks
If "!fn!" EQU 13 goto setvar
:setvar
set "coolestNum==1"
:thanks
cls
If "!coolestNum!"== 1 goto cool
echo Thanks
pause
goto :eof
:cool
echo cool
pause
goto :eof
That doesn't give an error, it just ignores the statement and keeps going as usual.
UPDATE:
After fixing errors this still doesn't work.
When I use exclamation marks it ignores the line, and when I use percentage signs it says:
"
"1 was not expected at this time"
One set of problems is your IF statements. For example, If "!coolestNum!"== 1 goto cool. The quotes are included in the comparison.
You need to be symmetric - either include quotes on both sides
If "!coolestNum!" == "1" goto cool
or neither:
If !coolestNum! == 1 goto cool
The same problem exists with If "!fn!" EQU 13 goto setvar, as well as the line before it.
The other problem you have is set "coolestNum==1" has an extra, unwanted =. The second = becomes part of the value. You only want two equal signs with IF comparisons.
Here is corrected code:
:start
#echo off
setlocal enabledelayedexpansion
title test
color a
cls
:favnum
cls
echo What is your favorite number?
set /p fn=Favorite Number
If "!fn!" NEQ "13" goto thanks
If "!fn!" EQU "13" goto setvar
:setvar
set "coolestNum=1"
:thanks
set coolest
cls
If "!coolestNum!"=="1" goto cool
echo Thanks
pause
goto :eof
:cool
echo cool
pause
goto :eof
But your logic is needlessly convoluted. The following produces the exact same result.
:start
#echo off
setlocal enabledelayedexpansion
title test
color a
cls
echo What is your favorite number?
set /p fn=Favorite Number
if !fn! equ 13 (
set "coolestNum=1"
echo cool
) else echo Thanks
pause
exit /b
Use
set variableTrue=1
rather than
set variableTrue EQU 1
This batch script:
#echo off
setlocal enabledelayedexpansion
cls
set variableTrue=1
echo A
if %variableTrue% EQU 1 goto next
echo B
goto :EOF
:next
echo C
outputs
A
C

Kick command for batch chat program

i am working on a batch chat program... because it's fun.. but i have run into a problem.
I want the users to be able to chat, or use commands from the same prompt and this method works:
set /p m=Message:
set tm="%m%"
if %tm% == "KICK" goto kick
echo %time% ^<%u%^>: %m% >> %log%
However to make the command work i need to have another step somewhere else in the batch file:
:kick
set /p person=Who to kick?
del %dir%\%person%
How can i make it so that someone can just type "KICK John" to kick someone?
set /p m=Message:
set tm=%m%
if /i "%tm:~0,4%"=="KICK" goto kick
echo %time% ^<%u%^>: %m% >> %log%
:kick
set person=%tm:~5%
if not defined person set /p person=Who to kick?
if not defined person goto kick
del %dir%\%person%
Naturally, the destination label for the final if not defined is debatable.
Fix : remove quotes surrounding %m%
Additional 20130924T0736Z
Here's my test batch:
:: #ECHO OFF
SETLOCAL
SET u=whatever
SET log=u:\log.log
set /p m=Message:
set tm=%m%
if /i "%tm:~0,4%"=="KICK" goto kick
echo %time% ^<%u%^>: %m% >> %log%
TYPE %log%
GOTO :eof
:kick
set person=%tm:~5%
if not defined person set /p person=Who to kick?
if not defined person goto kick
ECHO del %dir%\%person%
GOTO :EOF
And run-report:
>SETLOCAL
>SET u=whatever
>SET log=u:\log.log
>set /p m=Message:
Message:
>set tm=Hello Friend :P
>if /I "Hell" == "KICK" goto kick
>echo 15:33:12.77 <whatever>: Hello Friend :P 1>>u:\log.log
>TYPE u:\log.log
15:33:12.77 <whatever>: Hello Friend :P
>GOTO :eof
So - given that I've no idea of what your setting for u or log or dir happens to be, and that it works for me as shown above, I can only conclude that the quotes ARE required somewhere in the rest of your code - which you haven't shown.
There's no ban on using your quotes - once the presence of "kick" is determined, so
set tm="%m%"
directly after the IF /i... line should fix your problem.
Well, you could try this:
Setlocal Enableextensions
set /p m=Message:
set tm="%m%"
if /i %m:~0,4% == "KICK" call kick "%m:~4%"
echo %time% ^<%u%^>: %m% >> %log%
:kick
del %dir%\%1
goto :eof
Im not too familiar with this sort of Batch scripting (I'd normally use C#), but that should work fine. Tell me if it doesn't work, along with the errormessage. There are other ways of doing it as well.
Mona

assistance with my batch calculator

Can someone tell me what is wrong with my batch script? I just doesn't work...
#echo off
:start
echo please enter a mathamatical equation
set /p varia=
set /a %varia%
pause
goto start
If you go into command prompt and type in
set /a (9*5+4-9)/10
the next line is
4
So my idea is putting it into a batch script and use variables, but no good. I even tried just
set /a 5+5
but that failed too. For some reason the cmd command doesn't work in a batch file.
Instead try this:
#echo off
:start
echo Please enter a mathamatical equation:
set /p varia=
set /a answer=%varia%
echo =%answer%
pause
goto start
As you have turned echo off the variable's value won't be echoed.
Mona

Resources