Decimal to Hexadecimal & binary converter Batch program, Any suggestions how to implement it differently? - batch-file

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

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

Batch File Math (Subtraction) Stopped Working

I know that there must be some syntax issue on my end with this, but I can't for the life of me figure this out right now. This is a script I put together a few months back to automatically download some files for me as they release every month. It was working fine, however this month I noticed that nothing was downloading. After some troubleshooting and investigation, I found that the month variable was setting itself to "-1" instead of subtracting "1" from it's current value. As a result of this I'm unable to get the proper file name to attempt the download. It's the same match that I'm using on days, revision number, and year number, but for some reason the month variable just isn't cooperating with me and I can't figure out why.
:MASTER
#echo off
mode con:cols=100 lines=5
::Setup First Download
for /f "tokens=2*" %%a in ('REG Query "HKLM\SOFTWARE\Wow6432Node\ExampleRegistryKey" /v ExampleString 2^>nul') do set "ExampleDir=%%~b"
pushd "%ExampleDir%"
for /f "tokens=2 delims==" %%a in ('findstr SQLiteHome Example.ini') do set SQLiteHome=%%a
::Revision Number
set num=17
set /a "num=num-1"
::Begin set date
for /f "tokens=1-4 delims=/-. " %%i in ('date /t') do (call :set_date %%i %%j %%k %%l)
goto :end_set_date
:set_date
if "%1:~0,1%" gtr "9" shift
for /f "skip=1 tokens=2-4 delims=(-)" %%m in ('echo,^|date') do (set %%m=%1&set %%n=%2&set %%o=%3)
goto :eof
:end_set_date
set dd=31
This section here is where the month starts to equal -1 instead of current one subtracted from the current month.
set /a "mm=mm-1"
if %mm%==9 set mm=09
if %mm%==8 set mm=08
if %mm%==7 set mm=07
if %mm%==6 set mm=06
if %mm%==5 set mm=05
if %mm%==4 set mm=04
if %mm%==3 set mm=03
if %mm%==2 set mm=02
if %mm%==1 set mm=01
if %mm%==0 set /a "yy=yy-1"
if %mm%==0 set mm=12
Set EarlyEntry=early_up_sqlite_r
Set FullEntry=update_sqlite_r
Set MstarEntry=mstar_ext_sqlite_r
Set PSNEntry=psn_ext_sqlite_r
set CurrentEntry=%EarlyEntry%
set ThisFile=%CurrentEntry%%num%_%yy%%mm%%dd%.exe
ECHO %ThisFile%
set INIentry=EarlyLast
::Create %WorkingDirectory%\Dates.txt and Downloads.ini
set INIfile=C:\ProgramData\SA_Updater\Downloads.ini
set WorkingDirectory=C:\ProgramData\SA_Updater\
set OldDownloads=C:\ProgramData\SA_Updater\Downloads\
set PSNini=PSNLast
set MSTARini=MSTARLast
set Fullini=FullLast
set Earlyini=EarlyLast
echo %PSNini%
echo Dates > %WorkingDirectory%\Dates.txt
if not exist "%WorkingDirectory%" mkdir "%WorkingDirectory%"
if not exist "%OldDownloads%" mkdir "%OldDownloads%"
pusd %WorkingDirectory%
if not exist "%INIfile%" (
ECHO %PSNini%= > %INIfile%
ECHO %MSTARini%= >> %INIfile%
ECHO %Fullini%= >> %INIfile%
ECHO %Earlyini%= >> %INIfile%
)
:ObtainVariables
::Find the Last version downloaded
This is where I've kept a log for the download attempts
echo Dates > %WorkingDirectory%\Dates.txt
The output is looking like this:
Dates
http://Example.com/updates/early_up_sqlite_r16_2018-131.exe
http://Example.com/updates/early_up_sqlite_r15_2018-131.exe
http://Example.com/updates/early_up_sqlite_r14_2018-131.exe
Instead of:
Dates
http://Example.com/updates/early_up_sqlite_r16_20180731.exe
http://Example.com/updates/early_up_sqlite_r15_20180731.exe
http://Example.com/updates/early_up_sqlite_r14_20180731.exe
The rest of the script:
for /f "tokens=2 delims==" %%a in ('findstr %INIentry% %INIfile%') do set LastINIfile=%%a
ECHO %LastINIfile%
::Revision Number
set num=17
set /a "num=num-1"
::Begin set date
for /f "tokens=1-4 delims=/-. " %%i in ('date /t') do (call :set_date %%i %%j %%k %%l)
goto :end_set_date
:set_date
if "%1:~0,1%" gtr "9" shift
for /f "skip=1 tokens=2-4 delims=(-)" %%m in ('echo,^|date') do (set %%m=%1&set %%n=%2&set %%o=%3)
goto :eof
:end_set_date
set dd=31
set /a "mm=mm-1"
if %mm%==9 set mm=09
if %mm%==8 set mm=08
if %mm%==7 set mm=07
if %mm%==6 set mm=06
if %mm%==5 set mm=05
if %mm%==4 set mm=04
if %mm%==3 set mm=03
if %mm%==2 set mm=02
if %mm%==1 set mm=01
if %mm%==0 set /a "yy=yy-1"
if %mm%==0 set mm=12
set ThisFile=%CurrentEntry%%num%_%yy%%mm%%dd%.exe
goto Download
:Download
::Setup Download Variables
cls
set Download=http://Example.com/updates/%ThisFile%
GOTO TryDownload
:TryDownload
::Add Download Attempt to %WorkingDirectory%\Dates.txt
echo %Download% >> %WorkingDirectory%\Dates.txt
::Is the file download? If so, start the install
cls
mode con:cols=100 lines=10
powershell "Import-Module BitsTransfer; Start-BitsTransfer '%Download%' '%DownloadDir%'"
mode con:cols=100 lines=5
cls
::ping localhost -n 3 >nul
if exist C:\ProgramData\SA_Updater\%ThisFile% (
GOTO StartInstall
) else (
::Try Downloading
GOTO TryAgain
)
cls
:TryAgain
set /a "num=num-1"
if %mm%==9 set mm=09
if %mm%==8 set mm=08
if %mm%==7 set mm=07
if %mm%==6 set mm=06
if %mm%==5 set mm=05
if %mm%==4 set mm=04
if %mm%==3 set mm=03
if %mm%==2 set mm=02
if %mm%==1 set mm=01
if %num%==0 set /a "dd=dd-1"
if %dd%==27 set /a "mm=mm-1"
if %mm%==0 set /a "yy=yy-1"
if %mm%==0 set mm=12
if %dd%==27 set dd=33
if %num%==0 set num=17
set ThisFile=%CurrentEntry%%num%_%yy%%mm%%dd%.exe
set Download=http://Example.com/updates/%ThisFile%
set DownloadDir=%WorkingDirectory%%ThisFile%
if %ThisFile% EQU %LastINIfile% GOTO CheckEarly
GOTO TryDownload
)
:StartInstall
%WorkingDirectory%%ThisFile% /w /v"INSTALLPREREQUISITES=0"
robocopy %WorkingDirectory% %OldDownloads% %ThisFile%
pushd %WorkingDirectory%
del %ThisFile%
del /Q /A H *.tmp
GOTO DoINIstuff
::Update ThisFile
:CheckEarly
if %CurrentEntry% EQU %EarlyEntry% GOTO SetupFull
GOTO CheckFull
:CheckFull
if %CurrentEntry% EQU %FullEntry% GOTO SetupMstar
GOTO CheckMstar
:CheckMstar
if %CurrentEntry% EQU %MstarEntry% GOTO SetupPSN
GOTO CheckPSN
:CheckPSN
if %CurrentEntry% EQU %PSNEntry% GOTO SetupEarly
GOTO CheckEarly
::Setups
:SetupFull
set CurrentEntry=%FullEntry%
set INIentry=%Fullini%
GOTO ObtainVariables
:SetupMstar
set CurrentEntry=%MstarEntry%
set INIentry=%MSTARini%
GOTO ObtainVariables
:SetupPSN
set CurrentEntry=%PSNEntry%
set INIentry=%PSNini%
GOTO ObtainVariables
:SetupEarly
set CurrentEntry=%EarlyEntry%
set INIentry=%Earlyini%
GOTO ObtainVariables
:DoINIstuff
SetLocal EnableDelayedExpansion
Set _PathtoFile=%INIfile%
Set _OldLine=%INIentry%=
Set _NewLine=%INIentry%=%ThisFile%
Call :_Parse "%_PathtoFile%"
Set _Len=0
Set _Str=%_OldLine%
Set _Str=%_Str:"=.%987654321
:_Loop
If NOT "%_Str:~18%"=="" Set _Str=%_Str:~9%& Set /A _Len+=9& Goto _Loop
Set _Num=%_Str:~9,1%
Set /A _Len=_Len+_Num
PushD %_FilePath%
If Exist %_FileName%.new Del %_FileName%.new
If Exist %_FileName%.old Del %_FileName%.old
Set _LineNo=0
For /F "Tokens=* Eol=" %%I In (%_FileName%%_FileExt%) Do (
Set _tmp=%%I
Set /A _LineNo+=1
If /I "!_tmp:~0,%_Len%!"=="%_OldLine%" (
>>%_FileName%.new Echo %_NewLine%
) Else (
If !_LineNo! GTR 1 If "!_tmp:~0,1!"=="[" Echo.>>%_FileName%.new
SetLocal DisableDelayedExpansion
>>%_FileName%.new Echo %%I
EndLocal
))
Ren %_FileName%%_FileExt% %_FileName%.old
Ren %_FileName%.new %_FileName%.ini
PopD
Goto :CheckEarly
:_Parse
Set _FilePath=%~dp1
Set _FileName=%~n1
Set _FileExt=%~x1
Goto :EOF
I hope I haven't omitted too much to figure this out. Basically, I can see that the revision number and the day are acting like they should, but the month just reverts to "-1" instead of actually performing the subtraction, and I'm basically pulling my hair out trying to figure out what I'm doing wrong.
The reason is quite simple. A number string with a leading 0 is interpreted as octal number by C function strtol used by cmd.exe to convert a number string to an integer.
08 and 09 are invalid numbers in octal numeral system and for that reason function strtol returns 0 which is subtracted next by 1 resulting in -1.
The simple solution is using set /a "mm=1%mm%-101" instead of set /a "mm=mm-1". Then first the month value is concatenated as string with the character 1 building the strings 101 to 112 and so the number string has no leading 0 anymore and from this number 101 is subtracted to get 0 to 11 assigned as string to environment variable mm.
By the way: Use the following two command lines to get back the leading zero after subtraction:
set "mm=0%mm%"
set "mm=%mm:~-2%"
The first line concatenates 0-11 to 00-011 and the second line takes just the last two characters of this string resulting in 00-11 assigned finally to environment variable mm.
The next two lines should be replaced by: if %mm% == 00 set "mm=12" & set /a "yy-=1"

(Use a batch file) Get the filename only into a For loop

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

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.

Rubik's cube scrambler [closed]

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

Resources