Batch FOR loop variable repeat - loops

so my code is
FOR %%a IN (a b c) DO (FOR %%b IN (x y z) DO (echo %%a %%b &pause>nul))
but the output is
ax ay az bx by bz cx cy cz
i want one variable from each loop as ax by cz and noting else, please find it.

#echo off
setlocal EnableDelayedExpansion
rem Define first and second arrays
set i=0
for %%a in (a b c) do (
set /A i+=1
set first[!i!]=%%a
)
set i=0
for %%a in (x y z) do (
set /A i+=1
set second[!i!]=%%a
)
rem Show elements in both arrays with the same index
for /L %%i in (1,1,%i%) do echo !first[%%i]!!second[%%i]!
The same process with no arrays:
#echo off
setlocal EnableDelayedExpansion
set i=0
for %%a in (a b c) do (
set /A i+=1
set j=0
for %%b in (x y z) do (
set /A j+=1
if !i! equ !j! echo %%a%%b
)
)

You are doing it all wrong .... Try using a sequence of 1 1 3 where one is the step amount so that 1 1 3 would generate 1 2 3... The code is as follows
#echo off
For /L %%n in (1 1 3) do (
For %%c in (a b c) do (
Echo %%c%%n
)
)
Pause>null

Something like this:
#echo off
setlocal enabledelayedexpansion
set /A CNT=1
FOR %%a IN (a b c) DO (echo %%a !Cnt! &set /A Cnt+=1 &pause>nul)

#echo off
setlocal enabledelayedexpansion
set "a=cat dog horse"
set "b=bad little big"
:loop
set /a i+=1
set "x="
for /f "tokens=%i%" %%a in ("%b%") do set "x=%%a"<nul
if "%x%"=="" goto :eof
for /f "tokens=%i%" %%b in ("%a%") do ( echo %x% %%b)
goto :loop

Related

Windows Batch: How do you call a variable with concentric variable names and exclamation marks?

I would like to call variables that contain other variables in their name when I have enabledelayedexpension so I would have concentric exclamation points in my variable call.
I apologize for the unclear wording; I'm not very familiar with this subject.
Here's a portion of my code that shows my issue:
set /a Freq[%%z]value[!value!]+=%%y
echo Freq %%z value !value! is !Freq[%%z]value[!value!]!
As is, batch interprets !Freq[%%z]value[!value!]! broken up into different variables with the !'s, and I don't think I can use %'s instead because I'm in a for loop where this variable is changing. I don't think I can use another for loop to replace !value! with a %%a either.
Here's a more complete look at my code:
set /a line=0
set /a goodLine=0
FOR /F "tokens=* delims=" %%x in (%file%) DO (
set /a line+=1
echo Line is !line!
set data[!line!]=%%x
set /a value=0
set /a checkGood=0
FOR %%y in (%%x) DO (
set /a value+=1
if !value!==1 (
set /a Freq=%%y
set /a checkFreq=%%y %% 10
if !checkFreq!==0 (
set /a checkGood=1
) else (echo bad)
) else (
if !checkGood!==1 (
for /l %%z in (40, 10, 330) do (
if !Freq!==%%z (
set /a Freq[%%z]value[!value!]+=%%y
echo Freq %%z value !value! is !Freq[%%z]value[!value!]!
set /a Freq[%%z]quantity[!value!]+=1
echo Freq %%z value !value! quantity is !Freq[%%z]quantity[!value!]!
)
)
) else (echo checkGood bad)
)
)
)
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET /a value=4
FOR /L %%z IN (1,1,5) DO set /a Freq[%%z]value[!value!]=%%z+10
SET Freq
ECHO ========================
FOR %%y IN (2,7) DO FOR /L %%z IN (1,1,5) DO (
set /a Freq[%%z]value[!value!]+=%%y
SET Freq[%%z]value[!value!]
CALL echo Freq %%z value !value! is %%Freq[%%z]value[!value!]%%
)
GOTO :EOF
Or you could use a for/f "tokens=2delims==" %%e in ('set Freq[%%z]value[!value!]') do echo %%e
depends on what you really want to do.

How to reference the value of an environment variable with using another environment variable value in variable name?

I know that similar questions have been asked before and I have seen them but neither does !var[%Z%]! nor %var[!Z!]% work here:
#echo off
set Z=0
setlocal enabledelayedexpansion
set count=0
for /f "tokens=*" %%x in (Data) do (
set /a count+=1
set var[!count!]=%%x
)
:end
cls
echo %var[!Z!]%
choice /N /C QE
IF %errorlevel% == 1 GOTO ZP
IF %errorlevel% == 2 GOTO ZM
pause >nul
goto :end
:ZP
set /a Z=%Z%+1
goto :end
:ZM
set /a Z=%Z%-1
goto :end
I tried them and they don't work. What can I do?
Data is a file with following lines:
A
B
C
D
E
F
AA
AB
AC
AD
I'm assuming that this was your intention, (small modifications included). i.e. to be able to two way cycle through the data items using the Q and E keys.
Code as per the suggestion in my comment:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set /A "Z=count=0"
Setlocal EnableDelayedExpansion
For /F "Tokens=* UseBackQ" %%G In ("Data.ext") Do (
Set /A count += 1
Set "var[!count!]=%%G"
)
Set "count="
:End
ClS
Echo(!var[%Z%]!
%SystemRoot%\System32\choice.exe /C QE /N
If ErrorLevel 2 GoTo ZM
GoTo ZP
Pause 1>NUL
GoTo End
:ZP
Set /A Z += 1
GoTo End
:ZM
Set /A Z -= 1
GoTo End

Can't get value from array in BAT file

I have this code:
#echo off
setlocal EnableDelayedExpansion
rem FILL ARRAY
set n=0
for %%a in (A B C) do (
set fname[!n!]=%%a
set /A n+=1
)
rem FIRST OUTPUT
for /L %%i in (0,1,2) do (
echo !fname[%%i]!
)
echo/
rem SECOND OUTPUT
echo !fname[0]!
echo !fname[1]!
echo !fname[2]!
echo/
rem THIRD OUTPUT DOESN'T WORK
set n=0
for %%a in (A B C) do (
echo !fname[!n!]!
set /A n+=1
)
And get:
A
B
C
A
B
C
n
n
n
For some reasons I need output in third style and expect the same output like in first and second case but I can't understand what's wrong with it.
Update.
Thank you, folks. I guess I've confused you a little but really I need to use this output in variable, so I've found this working solution, maybe it'll help somebody else:
rem THIRD OUTPUT WORKS
set n=0
for %%a in (A B C) do (
for /f "tokens=2* delims==" %%x in ('set fname[!n!]') do (
<... using %%x...>
)
set /A n+=1
)
rem %%x contains output now and can be used anywhere
You need another layer of parsing.
call echo %%fname[!n!]%%
or
for %%n in (!n!) do echo !fname[%%n]!
cures your third case.
Incorporated into your script:
#echo off
setlocal EnableDelayedExpansion
rem FILL ARRAY
set n=0
for %%a in (A B C) do (
set fname[!n!]=%%a
set /A n+=1
)
rem skip first and second output
rem THIRD OUTPUT WORKS
set n=0
for %%a in (D E F) do (
call echo Copy "%%a" "%%fname[!n!]%%"
for %%n in (!n!) do ECHO cOpy "%%a" "!fname[%%n]!"
for /f "tokens=1* delims==" %%x in ('set fname[!n!]') do echo coPy "%%a" "%%y"
set /A n+=1
)
of course use just one of the three lines (and don't forget to remove the ECHO after troubleshooting)

Why is my batch code to get minimum value of an array not working?

I'm trying to retrieve the min value in an array that I have created calling this from another batch file.
Array has been created fine but the for /l is not working. I think, there is something with the if statement:
#echo off
for /f "usebackq" %%a in ('%2') do set d=%%~a
for /f "usebackq tokens=* delims=%d%" %%G in ('%3') do set %1=%%~G
set /a i=-1
for %%h in (!%1!) do (
set /a i+=1
set %1[!i!]=%%h
)
if %4==min (
set /a n=%i%
for /l %%j in (0,1,%n%) do (
if %%j==0 (
set %4=!%1[%%j]!
) else (
if !%1[%%j]! lss !%4! (
set %4=!%1[%%j]!
)
)
) else (
set %4="Please write the name of the function correctly"
)
:::: below the file im calling this function
#echo off
setlocal EnableDelayedExpansion
call test char "," "30,10,40" min
echo !min!
:: char is %1
:: "," is %2
:: "30,10,40" is %3
:: min is %4
pause
I rearranged your code in order to test it and modified some parts to fix a couple details. See my comments in UPPERCASE letters in the code:
#echo off
setlocal EnableDelayedExpansion
call :test char "," "30,10,40" min
echo !min!
:: char is %1
:: "," is %2
:: "30,10,40" is %3
:: min is %4
GOTO :EOF
:TEST
REM for /f "usebackq" %%a in ('%2') do set d=%%~a
REM Previous line is equivalent to this one:
set d=%~2
REM for /f "usebackq tokens=* delims=%d%" %%G in ('%3') do set %1=%%~G
REM I don't understand what you want to do in previous line,
REM but the next two lines replace the delimiter in %d% by spaces:
set %1=%~3
set %1=!%1:%d%= !
set /a i=-1
for %%h in (!%1!) do (
set /a i+=1
set %1[!i!]=%%h
)
if %4==min (
set /a n=%i%
REM The next line requires delayed expansion in !n! variable
REM or use %i% instead
for /l %%j in (0,1,!n!) do (
if %%j==0 (
set %4=!%1[%%j]!
) else (
if !%1[%%j]! lss !%4! (
set %4=!%1[%%j]!
REM The next right parentheses was missing
)
)
)
) else (
set %4="Please write the name of the function correctly"
)
EXIT /B

Batch File - Get list value using index key

I have written a script to bat file that will print the random value from list.
but I am facing problem to access list value using it's index
My code is:
set list=A B C D a b c
echo %list[3]%
for /l %%a in (1,1,6) do (
#set /a bottomlimit = 0
#set /a upperlimit = 5
#set /a num = !bottomlimit! + !RANDOM! %% !upperlimit! - !bottomlimit! + 1
echo %list[!num!]%
TIMEOUT /T 5
)
Waiting for your valuable solution.
Just three options. Number one to handle your approach. Number two for a "pure" array in environment variables. Number three will mix the two, definition of the list as in option 1, but iterating the list to generate the array in option 2.
#echo off
setlocal enableextensions enabledelayedexpansion
REM OPTION 1 - The list
echo -------------------------------------------------
setlocal
set "list=A B C D a b c"
set /a bottomlimit=0
set /a upperlimit=6
for /l %%a in (1,1,6) do (
set /a "num=!bottomlimit! + ( !RANDOM! %% (!upperlimit! - !bottomlimit! + 1))"
set "pos=0"
for %%l in (!list!) do if defined pos if !pos!==!num! ( echo %%l & set "pos=" ) else ( set /a "pos+=1")
)
endlocal
REM OPTION 2 - The "pure" array
echo -------------------------------------------------
setlocal
set "list[0]=A"
set "list[1]=B"
set "list[2]=C"
set "list[3]=D"
set "list[4]=a"
set "list[5]=b"
set "list[6]=c"
set /a bottomlimit=0
set /a upperlimit=6
for /l %%a in (1,1,6) do (
set /a "num=!bottomlimit! + ( !RANDOM! %% (!upperlimit! - !bottomlimit! + 1))"
for %%n in (!num!) do echo !list[%%n]!
)
endlocal
REM OPTION 3 - The remix
echo -------------------------------------------------
setlocal
set "list=A B C D a b c"
set "pos=0"
for %%l in (!list!) do ( set "list[!pos!]=%%l" & set /a "pos+=1" )
set /a "bottomlimit=0"
set /a "upperlimit=!pos!-1"
for /l %%a in (1,1,6) do (
set /a "num=!bottomlimit! + ( !RANDOM! %% (!upperlimit! - !bottomlimit! + 1))"
for %%n in (!num!) do echo !list[%%n]!
)
endlocal
endlocal
exit /b
#echo off
setlocal EnableDelayedExpansion
set list=A B C D a b c
set /a counter=1
for %%a in (%list%) do (
set "list[!counter!]=%%~a"
set /a counter=counter+1
)
set list[
for /l %%a in (1,1,6) do (
#set /a bottomlimit = 0
#set /a upperlimit = 5
#set /a num = bottomlimit + !RANDOM! %% upperlimit - bottomlimit + 1
for %%# in (!num!) do echo !list[%%#]!
TIMEOUT /T 5
)
endlocal
You may use echo/%list:~3,1%, but you can't use echo/%list:~%num%,1%
Try this:
#echo off
set "list=ABCDabc"
echo/%list:~3,1%
set /a bottomlimit = 0
set /a upperlimit = 5
for /l %%a in (1,1,6) do (
CALL:CALC
CALL _temp.bat
Timeout /t 5
)
del _temp.bat
pause >nul
exit/b
:CALC
set /a num = %bottomlimit% + %random% %% %upperlimit% - %bottomlimit% + 1
echo/set "list=%list%">_temp.bat
echo/echo/%%list:~%num%,1%%>>_temp.bat
exit/b

Resources