assistance with my batch calculator - batch-file

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

Related

SET /p crashing batch file process

In a batch file I'm getting information from the user for later database work.
I first ask if the user is using Windows Auth and then depending on the answer, I ask for username & password, or set a variable and continue on.
But for the life of me, I can't figure out why one of the SET /p lines is causing the batch file to crash, when another SET /p line isn't.
Here's my code:
#ECHO OFF
#SETLOCAL ENABLEDELAYEDEXPANSION
CHOICE /M "Will you be using Windows Authentication to connect to the database?"
ECHO ERRORLEVEL: %ERRORLEVEL%
#PAUSE
IF ERRORLEVEL 2 (
ECHO ERRORLEVEL should be 2: %ERRORLEVEL%
#PAUSE
SET _USINGWINAUTH=FALSE
SET /p _USERNAME=User name:
REM SET /p _PASSWORD=Password (NOTE: Password will be displayed as you type):
) ELSE (
ECHO ERRORLEVEL should be 1: %ERRORLEVEL%
#PAUSE
SET _USINGWINAUTH=TRUE
)
ECHO.
ECHO _USINGWINAUTH: %_USINGWINAUTH%
ECHO _USERNAME: %_USERNAME%
ECHO _PASSWORD: %_PASSWORD%
#PAUSE
As is, with the second SET /p commented out, regardless if you answer Y or N the script runs fine.
But if the second SET /p line is not commented, regardless of what the answer is, the script instantly closes right after hitting a key at the pause before the ECHO ERRORLEVEL.
I just don't see why that's happening!
As per last time, whilst using a caret works, it would be better if you were to rework the logic.
One example:
#ECHO OFF
CHOICE /M "Will you be using Windows Authentication to connect to the database"
IF "%ERRORLEVEL%"=="1" SET "_USINGWINAUTH=TRUE" & GOTO NEXT
SET "_USINGWINAUTH=FALSE"
SET /P "_USERNAME=User Name: "
SET /P "_PASSWORD=Password (will be displayed as you type): "
:NEXT
ECHO(
SET _USINGWINAUTH
SET _USERNAME 2>NUL
SET _PASSWORD 2>NUL
PAUSE

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

Delayed expansion with string manipulation and variable

The following command in a batch file returns the error
"1!==1 was unexpected at this time"
#echo off
setlocal enabledelayedexpansion
set num1=0
set/p "pass=>"
:start
set/a num1=%num1%+1
set/a num2=%num1%-1
if !pass:~%num2%,1!==1 set pass%num1%=1& goto start
The last line repeats once for every one digit number, the ==1 being replaced.
Since you don't say what you are entering as "pass", we're guessing.
Your if command should be
if "!pass:~%num2%,1!"=="1" set pass%num1%=1& goto start
which ensures that both sides of the operator == sontain non-empty strings.
That won't help your logic flaws, but it will cure the error report. You don't say clearly what you are trying to achieve, so beyond that...who knows?
You should use a temporary variable for your if command, like this:
#echo off
setlocal enableDelayedExpansion
set num1=0
set/p "pass=>"
:start
set/a num1=%num1%+1
set/a num2=%num1%-1
set "temppass=!pass:~%num2%,1!"
if [%temppass%]==[1] echo set pass%num1%=1& goto start
pause
The pause and the echo in the if are for debug-purposes and should probably be removed

I can't use spaces with the if statement

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

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

Resources