Error in If loop during execution of batch script - batch-file

I have received the below errors during the execution:
main.sh: line 9: syntax error near unexpected token (' main.sh: line
9:if %s3% LSS 0 ( '
Please have a look at the sample code:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set /a s1=10
set /a s2=20
set /a Var=0
set /a s3= %s1% - %s2%
if %s3% LSS 0 (
set s3=%s2%-%s1%
SET /a Var= (%s3%/%s1)*100
) else (
SET /a Var= (%s3%/%s2%)*100
)
pause
if "%Var%" GTR 15 goto failure
:failure
echo %Var%
pause

This is the way I would do it, that is, this code get the same result:
#echo off
SETLOCAL
set /a "s1=10, s2=20, Var=0, s3= s1 - s2, s3LSS0 = -(s3>>31)"
set /a "s3 = s3LSS0*(s2-s1) + !s3LSS0*s3, Var = (s3 / (s3LSS0*s1 + !s3LSS0*s2) )*100"
if %Var% GTR 15 goto failure
echo Ok: %Var%
pause
goto :EOF
:failure
echo Failure: %Var%
pause

Related

how to random array without duplicate using batch

Im using this batch for random test1-test3 without duplicate but not working
#echo off
setlocal EnableExtensions
setlocal EnableDelayedExpansion
set /A "RND_TOTAL=3, FLAG_DUP=0"
set /A "RND_MIN=1, RND_MAX=3, RND_INTER=1"
for /L %%I in (1,1,%RND_TOTAL%) do (
call :SUB %%I
SET /A R=!RND_NUM[%%I]!
SET LINE[1]=TEST1
SET LINE[2]=TEST2
SET LINE[3]=TEST2
echo !LINE[%R%]!
pause
)
endlocal
exit /B
:SUB
set /A "RND_COUNT=%1-1"
:LOOP
set /A "RND_NUM[%1]=!RANDOM!%%((RND_MAX-RND_MIN)/RND_INTER+1)*RND_INTER+RND_MIN"
if %FLAG_DUP% EQU 0 (
for /L %%I in (1,1,%RND_COUNT%) do (
if !RND_NUM[%1]! EQU !RND_NUM[%%I]! (
goto :LOOP
)
)
)
exit /B
I get the following output:
echo off
echo off
echo off

Set /a not working as 2 / 2

I couldn't get %difference_two% to work It can find %difference_one% as 2 but when I make it divide 2 by 2 it doesn't display as anything.
Use the numbers 2,5,10,17,26
To get what I need.
#echo off
color 0a
title Solver
:numbers
cls
set /p first=First:
set /p second=Second:
set /p third=Third:
set /p fourth=Fourth:
set /p fifth=Fifth:
goto solve
:solve
cls
set /a second_minus_first= %second% - %first%
set /a third_minus_second= %third% - %second%
if %third_minus_second%==%second_minus_first% (
goto s
) else (
goto d
)
:d
cls
set /a fourth_minus_third= %fourth% - %third%
set /a difference= %third_minus_second% - %second_minus_first%
set /a difference_one= %fourth_minus_third% - %third_minus_second%
if %difference%==%difference_one% (
set /a difference_two= %difference_one% / 2
set /a thing= %first% - %difference_two%
cls
echo %difference_two%n Squared + %thing%
pause >nul
goto numbers
) else (
goto wrong
)
Try this:
#echo off
setlocal EnableDelayedExpansion
color 0a
title Solver
:numbers
cls
set /p first=First:
set /p second=Second:
set /p third=Third:
set /p fourth=Fourth:
set /p fifth=Fifth:
goto solve
:solve
cls
set /a second_minus_first= %second% - %first%
set /a third_minus_second= %third% - %second%
if %third_minus_second%==%second_minus_first% (
goto s
) else (
goto d
)
:d
cls
set /a fourth_minus_third= %fourth% - %third%
set /a difference= %third_minus_second% - %second_minus_first%
set /a difference_one= %fourth_minus_third% - %third_minus_second%
if %difference%==%difference_one% (
set /a difference_two= !difference_one! / 2
set /a thing= !first! - !difference_two!
cls
echo !difference_two!n Squared + !thing!
pause >nul
goto numbers
) else (
goto wrong
)
In batch, commands in a code block (between ( )) are interpreted as on the same line, so you need to use setlocal EnableDelayedExpansion and use ! instead of % inside them.

How can I deal with this error in my Batch script?

I'm not sure what is happening as batch script closes almost immediately when I try to run it. It's pretty simple it should test whether a number(num) is a prime Number. If not it continues until num1 is bigger than num.
echo off
title Prime Numbers
cls
set /a prime=7
set /a num1=2
set /a num2=2
:do1
if %prime% == %num1% * %num2% goto do_if_1
if else %prime% LSS %num1% * %num2% set /a num2=%num2%+1
if else %prime% GTR %num1% * %num2% goto do_if_2
if else %prime% LSS %num1% * 1 goto do_if_3
goto do1
:do_if_1
set /a prime=%prime%+1
set /a num1=2
set /a num2=2
goto do1
:do_if_2
set /a num1=%num1%+1
set /a num2=2
goto do2
:do_if_3
echo %prime%
goto do_if_1
Edit: 6n±1 method
In case it's useful to anyone, here's a much more efficient method for testing whether a number is prime or composite. It's a Batch language adaptation of the 6n±1 algorithm mentioned by several people on this post.
#echo off & setlocal enabledelayedexpansion
if "%~1"=="" goto usage
for /f "delims=0123456789" %%I in ("%~1") do goto usage
if %~1 LEQ 1 (
echo %~1 is neither prime nor composite.
exit /b 1
) else if %~1 LEQ 3 (
echo %~1 is prime.
exit /b 0
)
set /a "mod2or3 = ^!(%~1 %% 2) | ^!(%~1 %% 3), half = %~1 / 2"
if %mod2or3% EQU 1 (
echo %~1 is composite.
exit /b 1
)
for /L %%I in (5,6,%half%) do (
set /a "n6pm1 = ^!(%~1 %% %%I) | ^!(%~1 %% (%%I + 2))"
if !n6pm1! EQU 1 (
echo %~1 is composite.
exit /b 1
)
)
echo %~1 is prime.
goto :EOF
:usage
echo Usage: %~nx0 integer
goto :EOF
Original answer:
Thought I might accept the challenge.
#echo off
setlocal enabledelayedexpansion
if "%~1"=="" goto usage
for /f "delims=0123456789" %%I in ("%~1") do goto usage
set /a limit = %~1 - 1
for /L %%I in (2,1,%limit%) do (
set /a modulo = %~1 %% %%I
if !modulo! equ 0 (
echo %~1 is composite ^(divisible by %%I^).
goto :EOF
)
)
echo %~1 is prime.
goto :EOF
:usage
echo Usage: %~nx0 integer
You had commented under Dominique's answer, "So how do I fix this?" Take the first part of your script:
set /a prime=7
set /a num1=2
set /a num2=2
:do1
if %prime% == %num1% * %num2% goto do_if_1
Since you've been informed by both Squashman and myself that if %prime% == %num1% * %num2% is not going to work as you intend, you should now know that you need to insert an additional set /a above your if statement to perform %num1% * %num2%.
set /a prime=7
set /a num1=2
set /a num2=2
:do1
set /a product = num1 * num2
if %prime% == %product% goto do_if_1
Let's continue...
...
if %prime% == %product% goto do_if_1
if else %prime% LSS %num1% * %num2% set /a num2=%num2%+1
There are two immediate problems here. #1, I think you probably meant else if, not if else. #2, in batch scripting, else needs to be included as a part of the if command you're else-ing. This means else either needs to be appended to the end of the previous line, or you should use some parentheses. Rewrite it like this:
if %prime% == %product% (
goto do_it_1
) else if %prime% LSS %product% (
set /a num2 += 1
) else etc...
The first thing I advise you, is to get rid of the echo off, like this you can follow what your script is doing:
D:\>set /a prime=7
D:\>set /a num1=2
D:\>set /a num2=2
D:\>if 7 == 2 * 2 goto do_if_1
7 was unexpected at this time. => this is causing your problem!
D:\>if else 7 LSS 2 * 2 set /a num2=2+1
D:\>

Multiplying error: * was unexpected at this time

I recently got a problem. I am making a game module in batch and I got a strange error:
Multiplying error "*!Map_PlayerLengthX! was unexpected at this time"
This is the whole code:
#echo off
setlocal enabledelayedexpansion
pause >nul
cls
call :Map_DefinePlayer 4 2 loloolol
echo %Px1y1%%Px2y1%%Px3y1%%Px4y1%
echo %Px1y2%%Px2y2%%Px3y2%%Px4y2%
pause >nul
:::::::::::::::::::::::::::
:: Map v0.10 By KKZiomek ::
:::::::::::::::::::::::::::
:Map_Init
set Map_Running=1
goto :eof
:Map_Load
if !Map_Running!==0 goto :eof
set Map_Load_FileToLoad=%~1
set Map_Load_BorderX=%~2
set Map_Load_BorderY=%~3
set Map_Load_BChar=%~4
set Map_Load_LineTotal=0
for /f "tokens=* delims= usebackq" %%l in (!Map_Load_FileToLoad!) do (
set /a Map_Load_LineTotal+=1
set Map_Line!Map_Load_LineTotal!=%%l
)
:Map_Load_ApplyCoords
for /l %%g in (1,1,!Map_Load_LineTotal!) do (
call :Map_Load_StrLen Map_Line%%g Map_Line%%g_Len
set /a Map_Load_ApplyCoords_DecidedLen+=!Map_Line%%g_Len!
)
set /a Map_Load_ApplyCoords_DecidedLen/=!Map_Load_LineTotal!
for /l %%y in (1,1,!Map_Load_LineTotal!) do (
for /l %%x in (1,1,!Map_Load_ApplyCoords_DecidedLen!) do (
set x%%xy%%y=!Map_Line%%y:~%%x,1!
)
)
goto :eof
:Map_Load_StrLen
setlocal disabledelayedexpansion
set Map_Load_StrLen_Len=0
if defined %~1 for /f "delims=:" %%n in (
'"(cmd /v:on /c echo(!%~1!&echo()|findstr /o ^^"'
) do set /a "Map_Load_StrLen_Len=%%n-3"
endlocal & if "%~2" neq "" (set %~2=%Map_Load_StrLen_Len%) else echo %Map_Load_StrLen_Len%
exit /b
:Map_Display
cls
for /l %%y in (1,1,!Map_Load_LineTotal!) do (
set Map_Display_Line%%y=
for /l %%x in (1,1,!Map_Load_ApplyCoords_DecidedLen!) do (
set Map_Display_Line%%y=!Map_Display_Line%%y!!x%%xy%%y!
)
)
for /l %%z in (1,1,!Map_Load_LineTotal!) do (
echo !Map_Display_Line%%z!
)
goto :eof
:Map_ReloadPos
set XPos=%~1
set YPos=%~2
set x!XPos!y!YPos!=!Map_Line%YPos%:~%XPos%,1!
goto :eof
:Map_DefinePlayer
set Map_PlayerLengthX=%~1
set Map_PlayerWidthY=%~2
set Map_DefinePlayer_Scheme=%~3
set /a Map_DefinePlayer_Modifier=!Map_PlayerLengthX!-1
for /l %%p in (1,1,!Map_PlayerWidthY!) do (
for /l %%q in (0,1,!Map_DefinePlayer_Modifier!) do (
set /a localq=%%q+1
set /a modq=%%q+((%%p-1)*!Map_PlayerLengthX!)
set Px%localq%y%%p=!Map_DefinePlayer_Scheme:~%modq%,1!
)
)
:::::::::::::::::::::::::::
:::::::::::::::::::::::::::
:::::::::::::::::::::::::::
I get the error in the function :Map_DefinePlayer. I think it's mainly in this line: set /a modq=%%q+((%%p-1)*!Map_PlayerLengthX!)
Every function works fine instead of this function because of this weird multiplying error. I tried enbling delayed expansion again, changing !Map_PlayerLengthX! into %Map_PlayerLengthX% but then it only changed the error in *4 was unexpected at this time
Anyone has an idea what causes this and how to fix it?
try with :
set /a "modq=%%q+((%%p-1)*!Map_PlayerLengthX!)"
the brackets in the expression are taken as closing brackets for the FOR loop

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