Number isn't added to variable in if statement - batch-file

Why doesn't this batch script add the value 5 to the variable %counter%?
The script always echo the value 2 with which the variable was initialized.
Outside a if statement the counter works just fine.
:start
set /a counter=2
set /p message=Message:
set "spam=%message%"
echo %spam%
if "%message%"=="%spam%" (
set /a counter=%counter% + 5
echo %counter%
)
pause
goto
start

You need to use delayedexpansion. %counter% is being evaluated outside of the If statement but not inside of it.
#echo off
setlocal enabledelayedexpansion
:start
set /a counter=2
set /p message=Message:
set "spam=%message%" echo %spam%
if "%message%"=="%spam%" ( set /a counter=%counter% + 5
echo !counter! )
pause
goto start

Related

local variable and return value of function in windows batch

#echo off
set /a n=99
echo before call test3
echo n=%n%
set ret=
call :test3 ret
echo after call test3
echo n=%n%
echo show array:
echo %ret.Array[0]%
echo %ret.Array[1]%
echo %ret.Array[2]%
echo %ret.Array[3]%
ECHO Press any key to close the windows...
pause>NUL
goto :eof
:test3
setlocal
set /a n=0
:Loop-Start
if %n% GEQ 3 goto :Loop-End
endlocal
set %~1.Array[%n%]=V%n%
setlocal
set /a n=n+1
goto :Loop-Start
:Loop-End
endlocal
goto :eof
Hi,
I have written a function test3 with local var named n, and test3 get a reference to variable named ret from caller as it parameter.
As I show in the code, I want my test3 to make an array has three elements, variable named ret in caller would hold the array.
But in caller when I print the array, I found I have not got three elements in the array.
who can help? thanks
Resolved. Following is working code for subroutine test3:
:test3
setlocal EnableDelayedExpansion
set /a n=0
for /l %%i in (0,1,3) do (
set s1=V!n!
if defined _ret (
set _ret=!_ret! ^&
)
set _ret=!_ret!set %~1.Array[!n!]=V!n!
set /a n=n+1
)
(
endlocal
%_ret%
)
goto :eof
Thanks

CMD add every variable generated to a list

I know I can write an echo > C:/Folder/name.txt
But I have a generator to make 2 number for Cord and can't figure out a quick way to make a one line echo > with all the points
for shortness I'll simplify my code because the current code is not the issue
Echo off
Setlocal EnableDelayedExpansion
set R=1
set Number=1
:Loop
if %R% EQU 1 (set /p Max=How many Max Points? ) Else(
echo.)
set /p PX%number%=What is PointX%number%?
set /p PY%number%=What is PointY%number%?
if %Number% GEQ %Max% (goto :fin) Else(
set /a Number=%Number%+1 & set R=2 & Goto :loop)
This Is what Im trying to Optimize
:fin
echo (%PX1%,%PY1%),(%PX2%,%PY2%),(%PX3%,%PY3%) ... Ect >C:Folder/File.txt
Is there any way to make all the generated numbers on 1 line
Credit to Squashman for the Answer - Thank you!
Correct Code:
Echo off
Setlocal EnableDelayedExpansion
set R=1
set Number=1
set Line=Test
:Loop
if %R% EQU 1 (set /p Max=How many Max Points? ) Else (
echo.)
set /p PX%number%=What is PointX%number%?
set /p PY%number%=What is PointY%number%?
set line=%line%(!PX%number%!,!PY%number%!),
if %Number% GEQ %Max% (goto :fin) Else (
set /a Number=%Number%+1 & set R=2 & Goto :loop)
:fin
echo %Line:~0,-1% >C:Folder/File.txt
Credit to Squashman for the Answer - Thank you!
The logic is so much simpler if you get the max point count before the loop. And the FOR /L loop can auto increment your counter and eliminate the need for GOTO.
#echo off
setlocal enableDelayedExpansion
set /p "cnt=How many points? "
set "ln="
for /l %%N in (1 1 %cnt%) do (
echo(
set /p "PX%%N=What is PointX%%N? "
set /p "PY%%N=What is PointY%%N? "
set "ln=!ln!(!PX%%N!,!PY%%N!),"
)
echo(
echo !ln:~0,-1!

Batch parenthesis trouble

I'm new in batch and have made a program that writes a file with the %num% variable.
I'm having problems with the sum. Instead of returning 1, 2 and 3, it returns (0+1),((0+1)+1) and (((0+1)+1)+1)...
Here is the code:
set num=0
:loop
set num=(%num%+1)
echo test > "%num%".txt
pause
goto loop;
you need the /a parameter to do arithmetics:
set /a num=%num%+1
shorter:
set /a num=num+1
even shorter:
set /a num+=1
Try it like below
#echo off
set num=0
:loop
set /a num=%num%+1
echo test > %num%.txt
pause
goto loop;

How to do math in batch-file

I have been having troubles with batch-codes that I would expect to work, but don't...
Below is what I have written...
#echo off
cls
:loop
set /p "input=Input a number: "
set /a "number=%input%" 2>nul
REM check if input valid
if "%input%" NEQ "%number%" (
cls
Echo Please Enter a valid number! &Echo.&Echo.
goto :loop
)
Set /a Even=number%%2
if %Even% EQU 0 (
Echo Substituting Even Number in: x / 2
Echo set /p"=(%number%) / 2 = "
set /a answer=number/2
) Else (
Echo Substituting Odd Number in: 3x - 1
<nul set /p"=3(%number%)-1 = "
set /a answer=number*3
set /a answer=answer-1
)
Echo %answer%
Echo.
Echo.
goto :loop
Echo Unexpected Error . . .
pause
Exit
Whenever I input a number into the console, it does the math, like I want it to, but prints the number -1, and every time i input another number, the number goes to -2, -3, -4, so on.
Put a setlocal enableextensions at the beginning after the #echo off, e.g.
#echo off
setlocal enableextensions
cls
Also, I think you would also need to use delayed variable expansion (usually denoted by !var!), which would change your script above to something like this:
#echo off
setlocal enableextensions enabledelayedexpansion
cls
:loop
set /p "input=Input a number: "
set /a number=!input! 2>nul
REM check if input valid
if "!input!" NEQ "!number!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
REM Make sure that it is an integer put in (just in case)
set /a int=!number! %% 1
if "!input!" NEQ "!int!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
Set /a Even=!number! %% 2
if !Even! EQU 0 (
Echo Substituting Even Number in: x / 2
set /a answer=!number! / 2
) Else (
Echo Substituting Odd Number in: 3x - 1
set /a answer=!number! * 3 - 1
)
Echo !answer!
Echo.
Echo.
goto :loop
I also would like to point out that I also fixed a few other bugs (set /p isn't of any use in this script at all, especially in where it is used, and also you need the modulus to find even/odd).

Batch 'FOR' Loop Parser

I have a small block of code that is supposed to parse through a file called data.dta but for some reason it keeps saying ( unexpected at this time, here is the code(I put a comment by the line that gives me an error):
:load
cd %appdata%\.Trek
FOR /f "eol=#" %%t IN (Resources\Data\data.dta) DO ( ::problem line
set count=1
set cor=0
FOR /f "tokens=1-2 delims=^=" %%f IN ("%%t") DO (
If %count% == 1 (
IF %%f==VERSION set cor=1
)
If %count% == 2 (
IF %cor%==1 (
set cor=0
set ver=%%f
)
)
set /a count=%count%+1
)
)
Title TREK Unmounted Console Version: %ver%
Contents of data.dta:
VERSION=ALPHA 2
I can spot one major problem, you need to enable delayed expansion for it to work.
In a code block, all variables will have the value of what they were set before the code block started. If you set a variable inside a code block, it will only have that value once it is outside of the code block.
Try this example:
#echo off
set var=1
echo %var%
for /l %%i in (1,1,5) do (
set /a var+=1
echo %var%
)
echo %var%
pause>nul
The output will be:
1
1
1
1
1
1
6
Now, try it with delayed expansions enabled, and replace the percent signs with exclamation marks:
#echo off
setlocal enabledelayedexpansion
set var=1
echo %var%
for /l %%i in (1,1,5) do (
set /a var+=1
echo !var!
)
echo %var%
Output:
1
2
3
4
5
6
6
So, change your code to:
:load
cd %appdata%\.Trek
setlocal enabledelayedexpansion
FOR /f "eol=#" %%t IN (Resources\Data\data.dta) DO (
set count=1
set cor=0
FOR /f "tokens=1-2 delims=^=" %%f IN ("%%t") DO (
If !count! == 1 (
IF %%f==VERSION set cor=1
)
If !count! == 2 (
IF !cor!==1 (
set cor=0
set ver=%%f
)
)
set /a count+=1
)
)
Title TREK Unmounted Console Version: %ver%
endlocal
...And see what happens now.

Resources