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
Related
I created my first batch file ever a couple of weeks ago to use livestreamer more comfortably.
Basically I can either type in the name of Twitch streamer or I can start something from another plattform.
What I am trying to do is is save my input as an option for the next time.
Let's say I went to the twitch stream of "shroud". When the stream ends I would like to put shroud as an option in the beginning of the script:
:start
title twitchings
color 0a
echo Select [#] or enter Stream
echo.
echo 1. non twitch
echo 2. shroud
set /p select=?
I assume it would be easiest to store that list in a txt file and then load it upon running the batch file, but my basically non-existant knowledge in coding hinders me from creating it.
Does anyone know a quick help, I would also love to know if there is a website where I can learn this stuff without googling for hours :)
This is my updated script for those who are searching for the same issue.
#ECHO off
:start
title twitchings
color 0a
echo Select [#] or enter Stream
echo.
echo 1. non twitch
setlocal enableextensions enabledelayedexpansion
set "file=C:\Users\[...]\savedstreams.txt"
set /A i=1
for /F "usebackq delims=" %%a in ("%file%") do (
set /a i += 1
echo !i! . %%a
)
set /p select=?
set /a varCheck=%select%
if %varCheck% == %select% (goto :isnumber) else (goto :isstream)
exit /B
:isnumber
set "lineNr=%select%"
set /a lineNr-=1+1
for /f "usebackq delims=" %%a in (`more +%lineNr% savedstreams.txt`) DO (
set "stream=%%a"
goto :leave
)
:leave
set "stream=%stream:*:=%"
echo stream: %stream%
goto qual
:isstream
set "stream=%select%"
echo %stream% >>savedstreams.txt
goto qual
:TpyeInSource
echo enter URL
echo.
set /p select2=?
livestreamer %select2%
GOTO end
:qual
livestreamer http://twitch.tv/%stream% 1080p60 || livestreamer http://twitch.tv/%stream% best
GOTO end
:end
#CHOICE /C:rqn /M "[R]etry, [Q]uit or [N]ew"
IF ERRORLEVEL 3 GOTO start
IF ERRORLEVEL 2 GOTO quit
IF ERRORLEVEL 1 GOTO qual
GOTO quit
:quit
echo "bye."
#PAUSE
Thank you guys!
You can use >> to put the output of a command in text file. For example:
echo hi everyone >>textfile.txt
will add "hi everyone" to the last line of the text file.
You can also use > to add it to the first line instead.
This question already has answers here:
Example of delayed expansion in batch file
(5 answers)
Closed 4 years ago.
I made a a database script to store giftcardcodes in. However, in my script the variables somehow don't contain anything. When I removed things that didnt matter in this question, I realised it worked.
WORKING:
::making variables
#echo off
title Please insert your code here:
cls
set /p code=Please insert your code here:
cls
title Who is the code from?
set /p client=Who is the code from?
cls
title For what did you get the money?
set /p what=For what did you get the money?
cls
title What's the amount of money saved on the code?
set /p amount=What's the amount of money saved on the code?
cls
::write to txt
#echo Code: %code%; Who: %client%; What: %what%; Money [in Euro]:%amount%>> %CD%\assets\database.txt
#echo off
color 0c
#echo Successfully written to database!
pause
exit
NOT WORKING:
:PASSWORDPROMPT
#ECHO off
setlocal EnableDelayedExpansion
#echo off
cls
#echo off
title Insert Password:
#echo off
set /p Passwort=Insert your Password here:
if %Passwort%==mypassword goto YES
if not %Passwort%==mypassword goto NO
:YES
if exist %CD%\assets\database.txt (
goto EXISTS
) else (
md %CD%\assets\
copy /b NUL %CD%\assets\database.txt
attrib +h %CD%\assets
)
:EXISTS
#echo off
title ENTER OR READ?
#echo off
cls
set /p readwrite=Do you want to ENTER a code to the database or READ the database? [ENTER/READ]
if /i %readwrite%==ENTER (
#echo off
title Please insert your code here:
cls
set /p code=Please insert your code here:
cls
title Who is the code from?
set /p client=Who is the code from?
cls
title For what did you get the money?
set /p what=For what did you get the money?
cls
title What's the amount of money saved on the code?
set /p amount=What's the amount of money saved on the code?
cls
#echo Code: %code%; Who: %client%; What: %what%; Money [in Euro]:%amount%>> %CD%\assets\database.txt
#echo off
color 0c
#echo Successfully written to database!
#echo off
color 0b
<nul set /p "=Press a key to Close..."
pause >nul
title Closing...
#echo off
for /L %%A in (3,-1,0) do (
cls
echo Closing in %%A Seconds.
ping localhost -n 2 >nul
cls
)
) else (
cls
#echo off
color
cls
#echo off
#echo ________________________________________________________
#echo YOUR CODES:
#echo ________________________________________________________
color
more %CD%\assets\database.txt
#echo ________________________________________________________
<nul set /p "=Press a key to Close..."
pause >nul
)
exit
:NO
cls
color 0c
#echo WRONG PASSWORD!
#echo off
timeout 1 /nobreak >null
#echo off
for /L %%A in (3,-1,0) do (
cls
echo Reload in %%A seconds...
ping localhost -n 2 >nul
)
cls
goto PASSWORDPROMPT
Does someone know what makes the difference and how I could fix the code?
You are experiencing the delayed-expansion issue. It seems that you have already enabled delayed expansion.
However, to preserve compatibility, we have to change the variables to be updated at runtime like so:
%var% -> !var!
Please note that it only applies to some variable. The following examples are invalid: !!~A, !1
For your situation, you should modify your code as so:
#echo Code: !code!; Who: !client!; What: !what!; Money [in Euro]:!amount!>> %CD%\assets\database.txt
I am currently coding (in batch) a mini tycoon game. However,
I'm having some difficulties when executing this command:
:cantbuy
echo Insufficient Funds!
pause
goto shop
:buy1
set /p money<"m2df.dll"
set /p cps<"m3df.dll"
if %money% GTR 100 goto canbuy1
goto cantbuy
:canbuy1
set /a "newmoney=%money%-100"
set /a "newcps=%cps%+2"
set cps=%newcps%
set money=%newmoney%
echo %money%>m2df.dll
echo %cps%>m3df.dll
echo Bought!
pause
goto shop
The program seems to exit straight after I input "1"
Any ideas how to fix this?
Update:
Found It Out. I decided to put a pause after everything to see what was wrong, and the syntax was bad and it went straight to another command crashing the script. here's the fixed script:
:cantbuy
echo Insufficient Funds!
pause
goto shop
:buy1
set /p money=<"m2df.dll"
set /p cps=<"m3df.dll"
if %money% GEQ 100 goto canbuy1
goto cantbuy
:canbuy1
set /a "newmoney=%money%-100"
set money=%newmoney%
set /a "newcps=%cps%+2"
set cps=%newcps%
echo %cps% >m3df.dll
echo %money% >m2df.dll
echo Bought!
pause
goto shop
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
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