I am writing a for loop in a batch file, which is to do arithmetic to a variable each iteration. The loop looks like this:
#echo off
setlocal enabledelayedexpansion
SET d=10
echo !d!
for /L %%t IN (0,1,9) DO (
SET /A d = %d% + 10
echo !d!
)
The arithmetic only is good for the first iteration. 'd' is to start at 10 and add by ten each time (10 20 30 ...) but it always stops at 20. In the output of the command prompt it will show:
10
20
20
...
20
20
How can I write this so it will add by ten for the entire loop?
You're close, but you missed using delayed expansion in one spot.
Change SET /A d = %d% + 10 to SET /A d = !d! + 10
#echo off
setlocal enabledelayedexpansion
SET d=10
echo !d!
for /L %%t IN (0,1,9) DO (
SET /A d = !d! + 10
echo !d!
)
Do as #JosefZ says if you need the academic exercise of arithmetic in a loop. If you want to get the same result with less code you can do this.
for /L %%t IN (20,10,110) DO echo %%t
Related
Here is my code:
for %%i in ("joined/*.mp4") do (
set /a result=(%random%*2/32768)+1
echo %result%
)
It gives me errors about +1 was unexpected at this time.
I tried another variant:
for %%i in ("joined/*.mp4") do (
set /a result=(%random%*2/32768)
echo %result%
)
It gives me an error about unbalanced parenthesis.
How can I echo the random variable correctly?
Thanks. :)
Trying the following code gives me the same value of random every time. How can I change it with each itertion of the loop?
setlocal EnableDelayedExpansion
for %%i in ("joined/*.mp4") do (
set /a result= %random%*20/32768 + 1
echo !result!
)
Is there a resource that I can read to learn in detail how batch files work and their language like loops, arrays etc.? I tried searching on Google but nothing useful came up.
Using brackets, (parentheses), is perfectly fine, and I would say more correct, so there's no reason why you cannot continue to do so.
Hare some options for you, which maintain their use:
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For %%G In ("joined\*.mp4") Do (
Set /A result = (!random! * 20 / 32768^) + 1
Echo !result!
)
Pause
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For %%G In ("joined\*.mp4") Do (
Set /A "result = (!random! * 20 / 32768) + 1"
Echo !result!
)
Pause
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For %%G In ("joined\*.mp4") Do (
Set /A result = (!random! %% 20^) + 1
Echo !result!
)
Pause
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For %%G In ("joined\*.mp4") Do (
Set /A "result = (!random! %% 20) + 1"
Echo !result!
)
Pause
As explained in the link provided by #Stephan, you'll need the ! on random too.
You can do it like this (for some reason, (!random!*20)/32768 triggers "/32768" was unexepected, so you have to split it on two lines :
setlocal EnableDelayedExpansion
for %%i in ("joined/*.mp4") do (
set /a result = !random!*20
set /a result= !result!/32768 +1
echo !result!
)
I am trying to create a matrix this one works below) but is slow as it has to loop every number. So i was wondering if you can have multiple %random% %% 2 without just a "(random number) %2" being printed.
#echo off
color 0a
:a
set /a mat=%random% %% 2
echo |set /p=%mat%
goto a
I found this to be much faster:
#echo off
if not "%1" == "max" start /MAX cmd /c %0 max & exit/b
color 0a
setlocal EnableDelayedExpansion
:a
set "addition="
for /L %%i in (1,1,256) do (
set /a mat=!random! %% 2
set addition=!addition!!mat!
)
echo |set /p=%addition%
goto a
EDIT: I now see that the answer #Stephan links to in the comments is very similar, so credit goes to him too.
EDIT #2:
You might like this one too, has all kind of characters so it looks a bit more "matrixy"
#echo off
if not "%1" == "max" start /MAX cmd /c %0 max & exit/b
color a
setlocal EnableDelayedExpansion
set "CHARS=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789######$$$%%%%%%*** "
echo %CHARS%>x&for %%? in (x) do set /a strlength=%%~z? - 2&del x
:a
call :randomString addition 100 "%chars%" %strlength%
echo |set /p=%addition%
goto a
:randomString
set "length=%2"
set "CHARS=%~3"
if ["%CHARS%"]==[""] set "CHARS=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789######$$$%%%%%%***"
set "strlength=%4"
if [%strlength%]==[] echo %CHARS%>x&for %%? in (x) do set /a strlength=%%~z? - 2&del x
set "line="
for /L %%a in (1 1 %length%) do (
set /a "randnr=!random!%%!strlength!"
for /l %%n in (!randnr! 1 !randnr!) do set "line=!line!!CHARS:~%%n,1!"
)
set %1=%line%
exit /b
For extra effect add 6 spaces to the CHARS variable. Note that I also added a line to start it in full-screen mode.
The method below is the fastest possible way to solve this problem:
#echo off
setlocal EnableDelayedExpansion
rem Prepare the output for one line
set "bin=0101010101"
set "line="
for /L %%i in (1,1,79) do (
set "line=!line!%%bin:~^!random:~-1^!,1%%"
)
for /L %%i in () do call echo %line%
When I use this code to select a character from my list, it works just fine but when I write it to a file using:
echo %pwd%>>pwd.gen
It will some times put the word "ECHO" randomly in the middle of the strings generated. Here is an example:
jUrkunjcxC
ecRECHOsI5w0T
DmJfat13fT
UWXOysW7Gb
pPmS7138Ve
nFkh32ECHOJd1
You can see it appears in line 2 and 6. This only happens about 20% of the time.
Here is the code:
#echo off
title Password Generator
color f0
:firstRun
set /a cnt=0
cls
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set /p Len=What length should the password be?
set /a Len=%Len%-1
cls
set /p Amt=How many would you like to generate?
cls
goto start
:start
set alfanum=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
set pwd=
FOR /L %%b IN (0, 1, %Len%) DO (
SET /A rnd_num=!RANDOM! * 62 / 32768 + 1
for /F %%c in ('echo %%alfanum:~!rnd_num!^,1%%') do set pwd=!pwd!%%c
)
echo %pwd%>> pwd.gen
set /a cnt=%cnt%+1
if %cnt%==%Amt% goto end
goto start
:end
cls
echo Done!
echo Results have been saved to "pwd.gen"
echo.
choice /c YN /m "Restart?"
if %errorlevel%==1 goto firstRun
:start
set alfanum=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
set pwd=
FOR /L %%b IN (0, 1, %Len%) DO (
SET /A rnd_num=!RANDOM! * 62 / 32768 + 1
for /F %%c in ('echo %%alfanum:~!rnd_num!^,1%%') do set pwd=!pwd!%%c
)
alfanum is 26+26+10 = 62 characters long.
RANDOM gives a random number from 0-32,767
When RANDOM is above 32240, rnd_num gets set to 62
string indexing starts at 0 not 1
the for /F %%c command indexes alfanum:~62,1~ which is an empty string
it calls echo with no parameter, which prints ECHO is on. instead of returning a single character
for /F defaults to splitting strings with a space delimiter, which separates out the first word
%%c becomes ECHO
you add ECHO into the password.
This is a combination of a couple of things. While I'm not totally clear about the inner workings of the whole thing, I know what's causing it and how to fix it.
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 is 62 characters long. However, substrings in batch start with 0, so valid numbers go from 0 to 61. For whatever reason, an index-out-of-range combined with the ^ in 'echo %%alfanum:~!rnd_num!^,1%%' is causing the word ECHO to be displayed.
To get around this, simply don't add 1 when calculating rnd_num.
SET /A rnd_num=!RANDOM! * 62 / 32768
I am trying to multiply two variables through a batch file. The code is:
SETLOCAL ENABLEDELAYEDEXPANSION
#ECHO OFF
SET /A res = 0
FOR /L %%i IN (1,1,2) DO (
FOR /L %%j IN (1,1,3) DO (
SET /A res = %%i * %%j
ECHO Multiplying %%i and %%j
ECHO %res%
)
)
The problem is that I always get 0 as a result. Can anyone please tell me what I am doing wrong?
Here's the output:
Multiplying 1 and 1
0
Multiplying 1 and 2
0
Multiplying 1 and 3
0
Multiplying 2 and 1
0
Multiplying 2 and 2
0
Multiplying 2 and 3
0
Thank you!
This is because you're outputting the result in the same block where you set it. Read up help set on delayed expansion. cmd expands %variables% when a command is parsed which is much earlier than when it's executed. As a side effect your %res% is replaced by zero.
You need
setlocal enabledelayedexpansion
at the start and then use !res! instead of %res%.
Side note: Ditch the spaces around the = in your set calls. They work as you expect when using them with /A but won't otherwise.
You need to use ! to expand the variable due to your delayed expansion setting:
SETLOCAL ENABLEDELAYEDEXPANSION
#ECHO OFF
SET res = 0
FOR /L %%i IN (1,1,2) DO (
FOR /L %%j IN (1,1,3) DO (
SET /A res = %%i * %%j
ECHO Multiplying %%i and %%j
ECHO !res!
)
)
i try to add two numbers received from a file.
But it shows only the last value of sum. Thx for the help!
#FOR /F "eol=; tokens=1-3 delims=, " %%i IN (test.txt) DO (
set m=%%j
set n=%%k
set /a sum=%m%+%n%
echo sum = %sum%
)
and in the test.txt i have
alex 4 5
john 6 7
and i want to see
sum=9
sum=13
it only shows
sum=13
sum=13
The problem is the percent expanding in the line set /a sum=%m%+%n% and echo sum = %sum%.
These are expanded before the FOR-loop is executed.
Therefore you got the result of the "global" set of sum.
It's better to use the delayed expansion, as then all variables enclosed with ! are expanded at runtime not parsetime
#echo off
setlocal EnableDelayedExpansion
FOR /F "eol=; tokens=1-3 delims=, " %%i IN (test.txt) DO (
set m=%%j
set n=%%k
set /a sum=m+n
echo sum = !sum!
)