I've got a set of variables called p1 to p9
I've got 2 vars to set the range I'm interested in.
I want to list all the vars from minimum range +1 to the max.
It looks like that (and it won't work)
set currentvar=5
set maxvar=9
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result = %maxvar% - %currentvar%
echo Found %result% vars in the range.
:LOOP
if %currentvar% LSS %maxvar% (
set /a currentvar=%currentvar% + 1
echo %p %currentvar% % //IT WON'T WORK AND I DON'T KNOW HOW TO MAKE IT WORK...
goto LOOP
) else (
goto END
)
:END
The result I'd like to see:
fff
ggg
hhh
iii
this might work for you:
#ECHO OFF &SETLOCAL
set /a currentvar=5
set /a maxvar=9
set /a RangeStart=currentvar+1
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result=maxvar-currentvar
echo Found %result% vars in the range.
for /l %%a in (%RangeStart% 1 %maxvar%) do call echo(%%p%%a%%
You need to Enabledelayedexpansion:
#echo off
Setlocal Enabledelayedexpansion
set currentvar=5
set maxvar=9
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result = %maxvar% - %currentvar%
echo Found %result% vars in the range.
:LOOP
if %currentvar% LSS %maxvar% (
set /a currentvar=%currentvar% + 1
echo !p%currentvar%! &REM This is how you make a comment in batch
goto LOOP
) else (
goto END
)
:END
Endlocal
And that should do what you want to do. Also to make a comment use a new line and type :: or REM.
Mona
Related
I'd like to start this off by saying batch scripting is something I ever do, and it's for an assignment in my class, so please bear with me. I am trying to take an array index and swap one of the indices for another. If I echo out each index after assigning it I get the expected output, but if I try to echo the array, the array hasn't changed. As I said I am very new with programming and especially batch, so I'm sure there is something fundamental I am missing.
my output
if index[0] is GTR index[4] if I enter 5,4,3,2,1:
echo %index[4]% outputs 5 %index[0] outputs 1
echo %numbers% outputs 5,4,3,2,1
my code
#echo off
set /p num1=Enter first num
set /p num2=Enter second num
set /p num3=Enter third num
set /p num4=Enter foruth num
set /p num5=Enter fifth num
SET /a num1=%num1%
SET /a num2=%num2%
SET /a num3=%num3%
SET /a num4=%num4%
SET /a num5=%num5%
SET numbers=%num1% %num2% %num3% %num4% %num5%
(for %%x in (%numbers%) do (echo %%x))
echo my array %numbers%
if %num1% GTR %num5% (
SET /A temp=%num1%
SET numbers[0]=%num5%
SET numbers[4]=%temp%
echo INDEX 4 is: %numbers[4]% INDEX 1 is: %numbers[0]%
) else (
echo "end of array is greater than start"
)
As in an above comment I needed to use setlocal enabledelayedexpansion
after doing this I found it easier to assign each index separately, and I did not test if would work the way I had the code in my question. ie set /a value[1] = value[1]
#echo off
setlocal enabledelayedexpansion
SET /p elem[0]=Enter a num
SET /p elem[1]=Enter a num
SET /p elem[2]=Enter a num
SET /p elem[3]=Enter a num
SET /p elem[4]=Enter a num
rem CHANGE TO INTEGER
SET /a elem[0]=elem[0]
SET /a elem[1]=elem[1]
SET /a elem[2]=elem[2]
SET /a elem[3]=elem[3]
SET /a elem[4]=elem[4]
rem loop through each value 0-4, echo each index value
for /l %%n in (0,1,4) do (
echo !elem[%%n]!
)
rem Check if value of elem[0] greater than elem[4], if so swap their positions
if %elem[0]% GTR %elem[4]% (
set /a temp = elem[0]
set /a elem[0] = elem[4]
set /a elem[4] = temp
echo Element 0 is greater than element 4 the new array is:
for /l %%n in (0,1,4) do (
echo !elem[%%n]!
)
rem If not greater, no swap needed
) else (echo Element 0 is NOT greater than element 4 the array is still the same)
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
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;
So I've been writing a batch file which backs up a certain file while cleaning old entries every so often. I've run into an issue where it would be simpler and more readable to store month lengths (for calculation purpose) (days1,days2,days3,etc.), and reference these by concatenating the word days with a variable which stores the month (1, 2, 3, etc.). Unfortunately, this never seems to reference the right variable correctly. Here's the relevant code, from a section calculating a date 28 days previously:
set days1=31
set days2=28
set days3=31
set days4=30
set days5=31
(etc.)
...
set pastmonthday=%curday%-28
set pastmonthmonth=%curmonth%
set pastmonthyear=%curyear%
if %pastmonthday% lss 0 (
set /a pastmonthmonth=%pastmonthmonth%-1
set /a pastmonthprevmon=1
)
if %pastmonthmonth%==0 (
set /a pastmonthyear-=1
set /a pastmonthmonth=12
)
set monthlengthvar=0
setlocal EnableDelayedExpansion
set tempmonthlengthvar=0
if %pastmonthday% lss 0 (set tempmonthlengthvar = !days%pastmonthmonth%!)
echo.%tempmonthlengthvar%
pause
for /F "delims=" %%A in (!tempmonthlengthvar!) DO (
endlocal
set "monthlengthvar=%%A"
)
set pastmonthday+=%monthlengthvar%
echo.%pastmonthday%
pause
...
The two echoes output 0 and -7, respectively. I can't figure out why this is, no matter how I've reworked it.
some errors (forgotten /a in set, quotes etc.)
#ECHO OFF &SETLOCAL disableDelayedExpansion
SET /a days1=31
set /a days2=28
set /a days3=31
set /a days4=30
set /a days5=31
set /a pastmonthday=22-28
set /a pastmonthmonth=2
set /a pastmonthyear=2014
if %pastmonthday% lss 0 (
set /a pastmonthmonth-=1
set /a pastmonthprevmon=1
)
if %pastmonthmonth% equ 0 (
set /a pastmonthyear-=1
set /a pastmonthmonth=12
)
set /a monthlengthvar=0
setlocal EnableDelayedExpansion
set /a tempmonthlengthvar=0
if %pastmonthday% lss 0 SET /a tempmonthlengthvar=!days%pastmonthmonth%!
ECHO(%tempmonthlengthvar%
for /F "delims=" %%A in ("%tempmonthlengthvar%") DO (
IF "!"=="" endlocal
set /a monthlengthvar=%%A
)
set /a pastmonthday+=monthlengthvar
ECHO(%pastmonthday%
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