I was able to increase the number variable:
SET /a Y=0
SET /a Y+=1
ECHO %Y% = 1
But I want to "increase" the variable with letters.
Ex:
SET Y=A
SET Y+=1
ECHO %Y% = B
Anyway to do something like this in Batch?
Only with a sort of fake pointer.
#Echo off
Setlocal EnableDelayedExpansion
Set "Letters=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Set I=25
SET Y=!Letters:~%I%,1!
Echo Y=%Y%
SET /A I+=1,I=I %% 26
SET Y=!Letters:~%I%,1!
Echo Y=%Y%
To have the algorythm continue with A again after reaching Z, you've to calculate the modulus 26
Y=Z
Y=A
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)
At first it said set was unexpected at this time and now when i run it it just closes this is in batch have any idea's? here's the code:
:waves
if %wave1%==1 set /a wave1=ACTIVATED
if %wave1%==0 set /a wave1=DEACTIVATED
if %wave2%==1 set /a wave2=ACTIVATED
if %wave2%==0 set /a wave2=DEACTIVATED
if %wave3%==1 set /a wave3=ACTIVATED
if %wave3%==0 set /a wave3=DEACTIVATED
if %wave4%==1 set /a wave4=ACTIVATED
if %wave4%==0 set /a wave4=DEACTIVATED
if %wave5%==1 set /a wave5=ACTIVATED
if %wave5%==0 set /a wave5=DEACTIVATED
if %wave6%==1 set /a wave6=ACTIVATED
if %wave6%==0 set /a wave6=DEACTIVATED
if %wave7%==1 set /a wave7=ACTIVATED
if %wave7%==0 set /a wave7=DEACTIVATED
if %wave8%==1 set /a wave8=ACTIVATED
if %wave8%==0 set /a wave8=DEACTIVATED
echo Type the number of the wave you like to change
echo wave # STATUS
echo -----------------------------------
echo wave 1 %wave1%
echo wave 2 %wave2%
echo wave 3 %wave3%
echo wave 4 %wave4%
echo wave 5 %wave5%
echo wave 6 %wave6%
echo wave 7 %wave7%
echo wave 8 %wave8%
choice /c 12345678
Running this I got:
if ==1 set /a wave1=ACTIVATED
This shows that %wave1% has no value and hence you've broken the if statement.
Use set %wave1=Whatever or add double quotes to the if
if "%wave1"=="1" set wave1=ACTIVATED
You also dont need /a as it's not arithmetic
I am trying to create a for loop in batch with some calculation using modulus from 1-100 then return an average for the entire set of numbers. I want to print this average on the screen and to a file called output This is what i have but it doesn't seem to be working correctly. Any suggestions? Thank you kindly.
#echo off
setlocal EnabledDelayedExpansion
for /l %%i in (1,1,100) do (
set /a EXPR = %%i %% 5
set /a EXPR2 = %EXPR+3
set /a TOTAL = TOTAL+%EXPR2
)
set /a AVG = %TOTAL/100
echo Your average is %AVG
echo Your average is %AVG >> output.txt
#echo off
setlocal EnableDelayedExpansion
for /l %%i in (1,1,100) do (
set /a EXPR=%%i %% 5
set /a EXPR2=EXPR+3
set /a TOTAL=TOTAL+EXPR2
)
set /a AVG=TOTAL/100
echo Your average is %AVG%
echo Your average is %AVG%
Is this what you want?
One additional d in EnableDelayedExpansion (btw. set /a works also without delayed expansion). Classical error is to let spaces surrounding variable assignment.In batch scripts result is that the space becomes part of the variable name.Except FOR tokens and command line arguments variables are two side enclosed with %
I am running this code:
#echo off
set /a var=0001
:start
if %var% == 10 set %var5%=00
if %var% == 100 set %var5%=0
if %var% == 1000 set %var5%=
if %var% == 9999 pause
set /a var=%var5%%var%+1
set var2=STRING %VAR%
set var3=ENTER
set var4=DELAY 1000
echo %var2%
echo %var3%
echo %var4%
#echo %var2%>> inject.txt
#echo %var3%>> inject.txt
#echo %var4%>> inject.txt
goto start
Which I made to generate a text file for a brute forcer that runs on a Usb rubber ducky. But, the if %var% == 10 set %var5%=00 Does not work. When it reaches 10, 100, or 1000 it does not add the 0000 to the beginning of the number. Please help, I'm using this for a friend that had his steam account hacked, and he needs to crack the 4 digit pin for the "parental controls"
Get rid of the %%around var5 in the set statement. You use the %% syntax only to read the value of an env var, not to assign.
The value assigned by set /a will suppresss leading zeroes; the hard way here is to use strings.
The easy way is this:
set /a var=10000
:loop
set "anothervar=%var:~-4%"
echo %anothervar%
set /a var+=1
if %var% lss 20000 goto loop
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