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.
Related
I wanted to ask this question for a long time. When I echo for example a box using hashtags, and put inside some text, this happens:
https://i.stack.imgur.com/0VnZq.png
And without the text, it looks normal:
https://i.stack.imgur.com/jyOw7.png
So I made a moving system in batch, and when I opened it it ended up looking like this.
https://i.stack.imgur.com/ZUxf4.png
Here's the code, thank you very much if you find an answer and a fix for this.
#echo off
#title batch rpg
cls
color 0f
:initsetup
set xc=3
set yc=3
:displaysetup
cls
set X1Y1=
set X1Y2=
set X1Y3=
set X1Y4=
set X2Y1=
set X2Y2=
set X2Y3=
set X2Y4=
set X3Y1=
set X3Y2=
set X3Y3=
set X3Y4=
set X4Y1=
set X4Y2=
set X4Y3=
set X4Y4=
:SCREEN
set X%xc%Y%yc%=#
echo ....
echo .%X1Y4%%X2Y4%%X3Y4%%X4Y4%.
echo .%X1Y3%%X2Y3%%X3Y3%%X4Y3%.
echo .%X1Y2%%X2Y2%%X3Y2%%X4Y2%.
echo .%X1Y1%%X2Y1%%X3Y1%%X4Y1%.
echo ....
choice /c wasd /n
set choice=%ERRORLEVEL%
goto choice%choice%
:choice1
set /A yc=%yc%+1
if %yc% GTR 4 (
set /A yc=%yc%-1
goto displaysetup
) ELSE (
goto displaysetup
)
:choice2
set /A xc=%xc%-1
if %xc% LSS 1 (
set /A xc=%xc%+1
goto displaysetup
) ELSE (
goto displaysetup
)
:choice3
set /A yc=%yc%-1
if %yc% LSS 1 (
set /A yc=%yc%+1
goto displaysetup
) ELSE (
goto displaysetup
)
:choice4
set /A xc=%xc%+1
if %xc% GTR 4 (
set /A xc=%xc%-1
goto displaysetup
) ELSE (
goto displaysetup
)
This is the new version according to your comments.
It still doesn't work.
I try to rename files like
2006 Gaspésie - Croisière à la baleine 1023.jpg
2006 Gaspésie - Croisière baleine 28.jpg
To
2006-06 Croisière baleine 001.jpg
2006-06 Croisière baleine 002.jpg
The problem is that the fields F1 and F2 are empty when I do echo and when I do rename.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set /p "NewFileName=New name file (without the extension): "
set /p "LenghtMask=Mask lenght of the number: "
set /a FileNo=0
for %%I in (*.JPG) do (
echo.
#echo off
set /a FileNo=!FileNo!+1
call :return_no_mask !FileNo! %LenghtMask% FileNoMask
#echo on
set "F1=%%~I"
echo F1 %F1%
set ExtensionName=%%~xI
set "F2=%NewFileName% %FileNoMask%%ExtensionName%"
echo F2 %F2%
rename "%F1%" "%F2%"
pause
)
goto:EOF
::--------------------------------------------------
::Return the number (fill with 0 to have the right lenght)
:: in Number to use
:: in Mask's lenght ex: 2
:: out Number
::--------------------------------------------------
:return_no_mask
set NoIn=%1
::---Search the lenght of the number
set /a LenghtNo=1
if "%NoIn%" GEQ "10" set /a LenghtNo=2
if "%NoIn%" GEQ "100" set /a LenghtNo=3
if "%NoIn%" GEQ "1000" set /a LenghtNo=4
if "%NoIn%" GEQ "10000" set /a LenghtNo=5
::---Fill with zero
set NoOut=
:test_add
if "%LenghtNo%" LSS "%2" (
set NoOut=0%NoOut%
set /a LenghtNo+=1
goto :test_add
)
set NoOut=%NoOut%%1
set %3=%NoOut%
EXIT /B 0
::--------------------------------------------------
::End of file
::--------------------------------------------------
:EOF
endlocal
Thank you
I recently had an issue with one of my batch programs, it is basically an incompleted platformer: My code is work in progress and i got stuck with my attemp of collision detection. I have a matrix M, that i can access within an EnableDelayedExpansion block like this !M[%%a][%%d]!
The first time it works, but the second usage does not work anymore:
echo A!M[%xx%,%yy%]!B yields AB.
I would appreciate any hint.
Thanks in advance!
#echo off
::creating a new line variable for multi line strings
set NLM=^
:: Two empty lines are required here
::set up initial grid
set height=9
set width=14
for /l %%a in (0,1,9) do (
for /l %%d in (0,1,14) do (
set M[%%a][%%d]=.
)
)
::create some vars at an initial value
set falling=0
set row=5
set turns=0
set x=8
set face=empty
:turn
set M[%row%][%x%]=^P
::set wall position
set x2=4
set y2=6
set M[%y2%][%x2%]=^#
::display current grid
setlocal EnableDelayedExpansion
set "grid="
for /l %%a in (0,1,9) do (
set line=!M[%%a][0]!
for /l %%d in (1,1,14) do (
set line=!line!!M[%%a][%%d]!
)
set grid=!grid! !NLM! !line!
)
cls
echo !grid!
endlocal
::prompt and make move
:: 4 = d
choice /c:wasd /n /m "" /t 1 /d s
set move=0
set /a xx=x
set /a yy=row
:move clearing Player
set M[%row%][%x%]=.
:: ^ this clears the characters trail so that there is no trail left behind after
:: moves, even when blocked. But at the start of grid Player is drawn again.
if %errorlevel%==4 ( if %x% LSS %width% ( set face=right
set /a xx=x+1) else ( set face=empty))
::if %errorlevel%==3 ( if %row% LSS %height% ( set face=down
::set /a move=0) else ( set face=empty))
if %errorlevel%==2 ( if %x% GTR 0 ( set face=left
set /a xx=x-1) else ( set face=empty))
if %errorlevel%==1 ( if %row% GTR 0 ( set face=up
set /a move=1) else ( set face=empty))
::falling!
set /a yy=yy-move
if %move%==0 (
set /a falling+=1
set face=down
) else (
set falling=0
)
set /a yy=yy+falling
::collision tests
setlocal enableDelayedExpansion
::echo !M[%xx%,%yy%]! > tmp.var
echo A!M[%xx%,%yy%]!B
endlocal
::set /P var1="" < tmp.var
::if %var1%==# (
:: set face=empty
::)
::now the actual movements
::if %face%==right set /a x+=1
::if %face%==left set /a x-=1
::if %face%==up set /a row=yy
::if %face%==down set /a row=yy
set /a x=xx
set /a row=yy
::increment turns, return to top
set /a turns+=1
goto :turn
Ive been working on this decimal to hexadecimal and binary converter, the hexadecimal converts the decimal number into hex, and the binary converter does it's job.
This works just fine, but is there a different way to implement this code?
#echo off
setlocal EnableDelayedExpansion
:Main
setlocal
cls
set /p decIn=enter Decimal value:
call :DectoHex %decIn% hexOut
call :DectoBin %decIn% binOut
echo !hexOut!
echo !binOut!
pause>nul
goto :main
endlocal
::-------------------------------------------------------------------------------------
:DectoHex
set /a A=%1
set map=0123456789ABCDEF
set H=
:Hexloop
set /a B=!A! %%16 & set /a A /=16
set H=!map:~%B%,1!!H!
if !A! gtr 0 goto :Hexloop
set %2=!H!
goto :eof
::-------------------------------------------------------------------------------
:DectoBin
:: Note Dec Bit Bin (DBB)
set /a Dec=%1
set Bin=
for /L %%i in (1,1,32) do (
set /a "Bit=Dec&1, Dec>>=1"
set Bin=!Bit!!Bin!
)
:skimming
if %Bin:~0,1% == 0 (
set Bin=%Bin:~1% & goto :skimming
)
set %2=!Bin!
goto :eof
::-----------------------------------------------------------------------------
:BintoDec
set bin=11011
set dig=
set digval=1
set dec=
:Loop
if %bin% gtr 1 (
set dig=%bin:~-1%
set bin=%bin:~0,-1%
) else (
set /a dig=%bin%
set bin=0
)
if %dig% equ 1 (
set /a dec+=%digval%
)
set digval *=2
if %bin% gtr 0 goto :loop
echo %dec%
pause>nul
goto :eof
The main function recieves an input from the user and passes the variable as a parameter to a hexadecimal converter and a binary converter, the
To convert to/from any number base between 2 and 36:
:: Q:\Test\2017\09\16\SO_46254352.cmd
#echo off & setlocal EnableDelayedExpansion
set map=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
:Main
cls
set "DecIn="
set /p DecIn=enter value:
if not defined DecIn goto :Eof
Set /A DecIn=DecIn
call :Dec2Base 2 %DecIn% BaseOut
call :Base2Dec 2 %BaseOut% DecBack
echo DecBack=%DecBack% Base 2=%BaseOut%
call :Dec2Base 16 %DecIn% BaseOut
call :Base2Dec 16 %BaseOut% DecBack
echo DecBack=%DecBack% Base 16=%BaseOut%
call :Dec2Base 32 %DecIn% BaseOut
call :Base2Dec 32 %BaseOut% DecBack
echo DecBack=%DecBack% Base 32=%BaseOut%
goto :main
::-------------------------------------------------------------------------------------
:Dec2Base Base DecIn BaseOut
Setlocal
Echo:%2|findstr /i "^[%map:~0,10%]*$" >Nul 2>&1 ||(Echo invalid char for base 10&Goto :Eof)
set /a Num=%2
set "Ret="
:Dec2BaseLoop
set /a "Digit=Num %% %1"
set /a "Num /= %1"
set Ret=!map:~%Digit%,1!%Ret%
if "%Num%" neq "0" goto :Dec2BaseLoop
Endlocal&Set "%3=%Ret%"&Goto :Eof
::-------------------------------------------------------------------------------------
:Base2Dec Base BaseIn DecBack
Setlocal EnableDelayedExpansion
Set /A "Base=%1,PlaceVal=1,Ret=0"
Echo:%2|findstr /i "^[!map:,%Base%!]*$" >Nul 2>&1 ||(Echo invalid char for base %1&Goto :Eof)
Set Val=%2
:Base2DecLoop
Set "Digit=%Val:~-1%"
If %Digit% Leq 9 goto :Next
For /L %%i in (10,1,%Base%) Do If /i "!Digit!" Equ "!map:~%%i,1!" (Set "Digit=%%i" & Goto :Next )
Echo Something went wrong & Pause
:Next
set /A "Ret+=Digit * PlaceVal,PlaceVal *= Base"
Set "Val=%Val:~0,-1%"
If defined Val goto :Base2DecLoop
Endlocal & Set "%3=%Ret%" & Goto :Eof
Sample output:
> Q:\Test\2017\09\16\SO_46254352.cmd
enter value: 4095
DecBack=4095 Base 2=111111111111
DecBack=4095 Base 16=FFF
DecBack=4095 Base 32=3VV
enter value: 0xBadAffe
DecBack=195932158 Base 2=1011101011011010111111111110
DecBack=195932158 Base 16=BADAFFE
DecBack=195932158 Base 32=5QRBVU
In a function, it gaves that.
(Put in in a UTF-8 .txt file with BOM EFBBBF, first line must be blank)
cls
#echo off
cd /d "%~dp0"
chcp 65001 >nul
set title_script=Base conversion
title %title_script%
set "decimal_number=200"
set "result_hexadecimal="
call :function_change_base_number 10 decimal_number 16 result_hexadecimal
echo %decimal_number% base 10 = %result_hexadecimal% base 16
echo.
set "octal_number=310"
set "result_binary="
call :function_change_base_number 8 octal_number 2 result_binary
echo %octal_number% base 8 = %result_binary% base 2
echo.
set "quaternary_number=3020"
set "base_36_result="
call :function_change_base_number 4 quaternary_number 36 base_36_result
echo %quaternary_number% base 4 = %base_36_result% base 36
echo.
pause
exit
:function_change_base_number
setlocal enabledelayedexpansion
set /a input_base=%~1
set input_number=!%~2!
set /a output_base=%~3
rem UCase
for %%i in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") do (
call set "input_number=!input_number:%%~i!"
)
set map=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
set map_base=!map:~0,%input_base%!
set /a position=0
set input_number_check=%input_number%
:loop_1
set current_map_base_character=!map_base:~%position%,1!
set input_number_check=!input_number_check:%current_map_base_character%=!
if "!input_number_check!" == "" goto next_1
set /a position+=1
if "!map_base:~%position%,1!" NEQ "" goto loop_1
echo Error.
echo Invalid character in base %input_base%.
echo.
pause
exit
:next_1
set /a carry_flag=1
set "input_number_tmp=%input_number%"
set "result_decimal="
:loop_2
set "input_number_tmp_digit=%input_number_tmp:~-1%"
if %input_number_tmp_digit% LEQ 9 goto next_2
for /l %%i in (10,1,%input_base%) do (
if /i "%input_number_tmp_digit%" EQU "!map:~%%i,1!" (
set "input_number_tmp_digit=%%i"
goto :next_2
)
)
echo Error.
:next_2
set /a result_decimal+=input_number_tmp_digit * carry_flag
set /a carry_flag *= input_base
set "input_number_tmp=%input_number_tmp:~0,-1%"
if defined input_number_tmp goto :loop_2
set /a input_number_tmp=%result_decimal%
set "result="
:loop_3
set /a "division_remainder=input_number_tmp %% !output_base!"
set /a "input_number_tmp /= !output_base!"
set result=!map:~%division_remainder%,1!%result%
if "%input_number_tmp%" NEQ "0" goto loop_3
endlocal & set %~4=%result%
goto :eof
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I made this awesome (horrible) Rubik's cube scrambler a while ago, but when I look at the code, it's just too much, I know there is a very simpler way of doing this. If there is anyone who can modify certain bits to make the code more compact, I would really appreciate it.
This is purely for my understanding in batch, and hopefully others can learn from anything you answer.
Here is the code:
#echo off
mode con: cols=62 lines=10
title Cube Scrambler
color 0a
Setlocal EnableDelayedExpansion
set ct = 0
:strt
set scram=BDEFLMRSUbdflru
set /a ct = ct +1
set /a a=%random%%%14
set /a b=%random%%%14
set /a c=%random%%%14
set /a d=%random%%%14
set /a e=%random%%%14
set /a f=%random%%%14
set /a g=%random%%%14
set /a h=%random%%%14
set /a i=%random%%%14
set /a j=%random%%%14
set /a k=%random%%%14
set /a l=%random%%%14
set /a m=%random%%%14
set /a n=%random%%%14
set /a o=%random%%%14
set a=!scram:~%a%,1!
set b=!scram:~%b%,1!
set c=!scram:~%c%,1!
set d=!scram:~%d%,1!
set e=!scram:~%e%,1!
set f=!scram:~%f%,1!
set g=!scram:~%g%,1!
set h=!scram:~%h%,1!
set i=!scram:~%i%,1!
set j=!scram:~%j%,1!
set k=!scram:~%k%,1!
set l=!scram:~%l%,1!
set m=!scram:~%m%,1!
set n=!scram:~%n%,1!
set o=!scram:~%o%,1!
set /a z1=%random%%%2 +1
set /a z2=%random%%%2 +1
set /a z3=%random%%%2 +1
set /a z4=%random%%%2 +1
set /a z5=%random%%%2 +1
set /a z6=%random%%%2 +1
set /a z7=%random%%%2 +1
set /a z8=%random%%%2 +1
set /a z9=%random%%%2 +1
set /a z10=%random%%%2 +1
set /a z11=%random%%%2 +1
set /a z12=%random%%%2 +1
set /a z13=%random%%%2 +1
set /a z14=%random%%%2 +1
set /a z15=%random%%%2 +1
if %z1% == 2 goto 2x2
set x1='
:2x2
if %z2% == 2 goto 3x3
set x2='
:3x3
if %z3% == 2 goto 4x4
set x3='
:4x4
if %z4% == 2 goto 5x5
set x4='
:5x5
if %z5% == 2 goto 6x6
set x5='
:6x6
if %z6% == 2 goto 7x7
set x6='
:7x7
if %z7% == 2 goto 8x8
set x7='
:8x8
if %z8% == 2 goto 9x9
set x8='
:9x9
if %z9% == 2 goto 10x10
set x9='
:10x10
if %z10% == 2 goto 11x11
set x10='
:11x11
if %z11% == 2 goto 12x12
set x11='
:12x12
if %z12% == 2 goto 13x13
set x12='
:13x13
if %z13% == 2 goto 14x14
set x13='
:14x14
if %z14% == 2 goto 15x15
set x14='
:15x15
if %z15% == 2 goto ans
set x15='
:ans
echo.
echo Scramble: %ct%
echo.
echo.
echo %a%%x1% %b%%x2% %c%%x3% %d%%x4% %e%%x5% %f%%x6% %g%%x7% %h%%x8% %i%%x9% %j%%x10% %k%%x11% %l%%x12% %m%%x13% %n%%x14% %o%%x15%
echo.
echo.
echo { Enter } = New scramble
echo Made by: Ruan Swanepoel
pause>nul
cls
set x1=
set x2=
set x3=
set x4=
set x5=
set x6=
set x7=
set x8=
set x9=
set x10=
set x11=
set x12=
set x13=
set x14=
set x15=
goto strt
Maybe something like this approach using FOR loops?
#ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion
mode con: cols=62 lines=10
title Cube Scrambler
color 0a
set "ct=0"
set "discontinue="
:strt
set "scram=BDEFLMRSUbdflru"
set /a "ct+=1"
for %%x in (a b c d e f g h i j k l m n o) do (
set /a "xx=!random!%%15"
call set "%%x=%%scram:~!xx!,1%%"
)
for /L %%y in (1, 1, 15) do (
set /a "yy=!random!%%2"
if !yy! EQU 1 (
set "x%%y= "
) else (
set "x%%y='"
)
)
:ans
echo(
echo Scramble: %ct%
echo(
echo(
echo %a%%x1% %b%%x2% %c%%x3% %d%%x4% %e%%x5% %f%%x6% %g%%x7% %h%%x8% %i%%x9% %j%%x10% %k%%x11% %l%%x12% %m%%x13% %n%%x14% %o%%x15%
echo(
echo(
echo { Enter } = New scramble
echo Made by: Ruan Swanepoel
set /P "discontinue="
if defined discontinue goto :endlocal
cls
goto strt
:endlocal
color
title %CD%
rem mode con: cols=80 lines=25
ENDLOCAL
goto :eof
It could probably be trimmed down a bit more (especially if I wanted to tweak the spacing), but I got it down to 29% of what it was, so there's that.
#echo off
setlocal enabledelayedexpansion
mode con cols=62 lines=10
title Cube Scrambler
color 0A
set ct=0
:begin
set scram=BDEFLMRSUbdflru
set /a ct+=1
for /l %%A in (1,1,15) do (
set /a mod_15=!random!%%15
for /F "delims=" %%B in ("!mod_15!") do (
set mov[%%A]=!scram:~%%B,1!
)
set /a inv[%%A]=!random!%%2
if !inv[%%A]! equ 0 (
set "inv[%%A]= "
) else (
set "inv[%%A]=' "
)
)
echo.
echo Scramble: !ct!
echo.
for /l %%A in (1,1,15) do (
set /p"=!mov[%%A]!!inv[%%A]!"<nul
)
echo.
echo.
echo { Enter } = New scramble
echo Made by: Ruan Swanepoel
pause>nul
cls
goto begin
This version is even smaller (just above 18% of the original) at the expense of some readability:
#echo off
setlocal enabledelayedexpansion
mode con cols=45 lines=10
set c=0
set s=BDEFLMRSUbdflru
:a
set /a c+=1
title Cube Scramble !c!
for /l %%A in (1,1,15) do (
set /a r=!random!%%15,i%%A=!random!%%2
for /F %%B in ("!r!") do set m%%A=!s:~%%B,1!
if !i%%A! equ 0 (set i%%A= ) else (set i%%A=' )
)
echo.
for /l %%A in (1,1,15) do set /p=!m%%A!!i%%A!<nul
echo.
echo Enter: Rescramble
pause>nul
cls
goto a