How to count arrays in FOR loops using a variable as index? - arrays

Okay. I got rid of most of the useless code and have been tinkering with it here and there trying to find what is the problem and I think I finally found it:
Windows' Batch can't edit variables that will be expanded if those are inside a FOR loop.
Ex:
set /a x=1
Powershell Get-Clipboard> %temp%\ffmpeglist.txt
setlocal enableExtensions enableDelayedExpansion
for /F "delims=| tokens=*" %%A in (%temp%\ffmpeglist.txt) do (
set input[!x!]=%%A
call echo !input[%x%]!
set /a x += 1
)
endlocal
Expected behavior:
g:\videos\youtube1.mp4
g:\videos\youtube2.mp4
g:\videos\youtube3.mp4
g:\videos\youtube4.mp4
g:\videos\youtube5.mp4
What I get:
g:\videos\youtube1.mp4
g:\videos\youtube1.mp4
g:\videos\youtube1.mp4
g:\videos\youtube1.mp4
g:\videos\youtube1.mp4
No matter what I do, set /a x+= 1 will not change the value of x.
Are there solutions? I'm open to anything.

EDIT
In your heavily changed batch (in fact a new question) change
call echo !input[%x%]!
to
call echo %%input[!x!]%%
If in a (code block) you might need delayed expansion to force actual values,
if at the same time using an indexed variable you need another level of delayed expansion you can accomplish with a pseudo call and doubled percent signs
Call set Input=%%input[!x!]%%
mediainfo --Output=Video;%%Height%% !input! > %temp%\ffmpegres%x%.txt

Thanks to #Aacini and #LotPings , I've arrived at the answer:
set /a x=1
Powershell Get-Clipboard> %temp%\ffmpeglist.txt
setlocal enableDelayedExpansion
for /f "delims=|" %%A in (%temp%\ffmpeglist.txt) do (
set input[!x!]="%%A"
for %%i in (!x!) do echo !input[%%i]!
set /a x =+ 1
)
The problem wasn't that BATCH couldn't
edit variables that will be expanded if those are inside a FOR loop.
But that it does it in a not very straight forward way.

Related

set /A command expands with a comma for more than one var, only set does not

(set /a "m1=1,m2=2")
for /f %%c in ("%m1%%m2%") do echo %%c
pause
The brackets else where than due to the for command are used in cases, a space key should have been added.
The echo of the for command, is 12. I used the number characters to face the set /A command with decimal Expression.
When i try the same procedure only with a set for a Shell, may also be named m1 it is just possible without comma seperation.
With set command the m1 Expression would be 1 m2 2 and not two values like with a set /A SET.
Is there a way to use set only once and not only with the set /A?
As other answers and comments already indicated, there is no way to directly do this in one command, but via a procedure. The method below is the simplest one:
#echo off
rem Define the several values
set "vars=m1=1,m2=2"
rem Do it:
set "%vars:,=" & set "%"
echo m1=%m1%
echo m2=%m2%
You may remove the #echo off command and execute this program to see what exactly is executed...
As I understand the question you want something like:
set x=1,y=2
and as a result to have two variables (like set /a). The answer is no.
Though you can iterate trough expressions with plain for :
#echo off
for %%a in (
"x=1" "a=5"
"y=2" "b=6"
"z=3" "c=7"
) do set "%%~a"
echo %x% %y% %z% %a% %b% %c%
Mind that the quotes around the items are mandatory because = is a delimiter. You can put everything on line and to use as separators , ,; ,<space>
May be with a lot of variables this can save you from some writing...?
this can be rewritten like this:
#echo off
set "vars=x=1,y=2,z=3,a=5,b=6,c=7"
for %%a in ("%vars:,=","%") do set "%%~a"
echo %x% %y% %z% %a% %b% %c%
And thus you'll need to change only the vars value.

Batch nested variables

So, I have some nested variables in Batch.
It's causing me an issue.
The reason I have nested variables is because I am emulating an array via:
array[0]=1
array[1]=2
array[2]=3
etc
Now, I have a counter counter
I want to be able to do %array[!counter!]%, and the output would equal !counter! + 1.
But I can't.
I think it's because having it surrounded in %'s makes Windows try to expand the variable ASAP, and it does, breaking the counter.
So, instead, why not do !array[!counter!]!? Well, I tried this, and I think that, instead of interpreting it as (array[(counter)]), where ()'s are used to show what !!'s are holding, Windows instead interprets it as (array[)counter(), which is useless to me.
Keep in mind: Whenever I use !!'s, assume I have done setlocal EnableDelayedExpansion -- I just don't include it as that would be a pain for both me and readers.
Any ideas how to fix this?
(at least) Two possible ways. The first is faster and more verbose - CALL command hits the performance.
#echo off
setlocal enableDelayedExpansion
set array[0]=1
set array[1]=2
set array[2]=3
set counter=0
echo first way :
for /l %%# in (1;1;3) do (
for /f %%$ in ("!counter!") do echo !array[%%$]!
set /a counter=counter+1
)
set counter=0
echo second way :
for /l %%# in (1;1;3) do (
call :subr1 !counter!
set /a counter=counter+1
)
goto :eof
:subr1
echo !array[%1]!
goto :eof
This is what I ended up doing:
for /l %%a in (1;1;3) do (
echo !array[%%a]!
)
Originally, I was using a manual counter via variable counter, but using for /l means I can have a counter without having to set it to a variable, and, more importantly, not calling it like !varname!; instead like %%varname, eliminating confusion.

Batch map and lookup with expanded variables

I'm trying to do this, but my "v" variable is !expanded!. Augh. I've tried flipping things around but I'm not very good with expansion. How could I adjust the map and lookup to work with an expanded variable?
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set map=mon;Monday;tue;Tuesday;wed;Wednesday;thu;Thursday;fri;Friday;sat;Saturday;sun;Sunday
FOR /f "delims=" %%i IN (q20764599.txt) DO (
SET "v=%%i"
CALL :setv
ECHO ==!v!==
)
ECHO +%v%+
GOTO :EOF
:setv
CALL SET v=%%map:*%v%;=%%
SET v=%v:;=&rem.%
GOTO :eof
This should simulate your wanting to work with !v!. The file q20764599.txt could contain say a single line reading tue which gets assigned to v and the magic proceeds from there...

not able to read characters of a string inside a for loop

Am trying to read characters of a string inside a for loop.
The command !string:~1,3! works fine. But can I do this with variables instead of 1 and 3. I tried the following code, but I don't know what is wrong. Its not working.
#echo off
setlocal enableextensions enabledelayedexpansion
set string=abcdefghij
set /a count=1
for /l %%x in (1,1,3) do (
set string2=!string:~%count%,1!
set /a count+=1
echo !string2!
pause
)
but It always gives the output as:
b
I want the output to be as:
b
c
d
Kindly help in solving this.. A big thanks in advance
In order to achieve what you want, you need to do a Delayed Expansion twice, that is, something like this:
set string2=!string:~!count!,1!
Of course, previous line is invalid. Although there are several ways to solve this problem, most of they use call command that is slow. To fix this problem so it run in the fastest way use a for command to change the first !count! expansion into a FOR replaceable parameter, and then use it in the original expression:
for %%i in (!count!) do set string2=!string:~%%i,1!
The problem is that the expansion of %count% isn't delayed, so it has the same value for every loop iteration. It's better written like this:
#echo off
setlocal enableextensions enabledelayedexpansion
set string=abcdefghij
set /a count=1
for /l %%x in (%count%,1,3) do (
set string2=!string:~%%x,1!
echo !string2!
)
Edit
If you want to keep %count% evaluated as the variable is set and not just at the beginning of the for loop, use Aacini's answer.

Batch - Read contents of a file in an array

I've a text file with two rows (say param.txt) which is shown below:
Mar2012
dim1,dim2,dim3,dim4
I want to read this file in batch and store the contents of first line in a variable called cube_name. When I'm reading the second line, I want to split the comma delimited string dim1,dim2,dim3,dim4 and create an array of four elements. I am planning to use the variable and the array in later part of the script.
The code which I created is shown below. The code is not working as expected.
#echo off & setlocal enableextensions enabledelayedexpansion
set /a count_=0
for /f "tokens=*" %%a in ('type param.txt') do (
set /a count_+=1
set my_arr[!count_!]=%%a
)
set /a count=0
for %%i in (%my_arr%) do (
set /a count+=1
if !count! EQU 1 (
set cube_name=%%i
)
if !count! GTR 1 (
set dim_arr=%%i:#=,%
)
)
for %%i in (%dim_arr%) do (
echo %%i
)
echo !cube_name!
I get to see the following when I run the code:
C:\Working folder>test2.bat
ECHO is off.
So this doesn't appear to work and I can't figure out what I'm doing wrong. I am fairly new to the batch scripting so help is appreciated
Your first FOR loop is OK. It is not how I would do it, but it works. Everything after that is a mess. It looks like you think arrays are a formal concept in batch, when they are not. It is possible to work with variables in a way that looks reminiscent of arrays. But true arrays do not exist within batch.
You use %my_arr% as if it is an array, but my_arr is not even defined. You have defined variables my_arr[1] amd my_arr[2] - the brackets and number are part of the variable name.
It also looks like you have a misunderstanding of FOR loops. I suggest you carefully read the FOR documentation (type HELP FOR from a command line). Also look at examples on this and other sites. The FOR command is very complicated because it has many variations that look similar to the untrained eye, yet have profoundly different behaviors. One excellent resource to help your understanding is http://judago.webs.com/batchforloops.htm
Assuming the file always has exactly 2 lines, I would solve your problem like so
#echo off
setlocal enableDelayedExpansion
set dimCnt=0
<param.txt (
set /p "cube_name=" >nul
set /p "dimList=" >nul
for %%D in (!dimList!) do (
set /a dimCnt+=1
set "dim[!dimCnt!]=%%D"
)
)
echo cube_name=!cube_name!
for /l %%I in (1 1 !dimCnt!) do echo dim[%%I]=!dim[%%I]!
One nice feature of the above solution is it allows for a varying number of terms in the list of dimensions in the 2nd line. It will fail if there are tabs, spaces, semicolon, equal, * or ? in the dimension names. There are relatively simple ways to get around this limitation if need be.
Tabs, spaces, semicolon and equal can be handled by using search and replace to enclose each term in quotes.
for %%D in ("!dimList:,=","!") do (
set /a dimCnt+=1
set "dim[!dimCnt!]=%%~D"
)
I won't post the full solution here since it is not likely to be needed. But handling * and/or ? would require replacing the commas with a new-line character and switching to a FOR /F statement.
I'm impressed of your code!
Do you try to debug or echo anything there?
You could simply add some echo's to see why your code can't work.
#echo off & setlocal enableextensions enabledelayedexpansion
set /a count_=0
for /f "tokens=*" %%a in ('type param.txt') do (
set /a count_+=1
set my_arr[!count_!]=%%a
)
echo ### show the variable(s) beginning with my_arr...
set my_arr
echo Part 2----
set /a count=0
echo The value of my_arr is "%my_arr%"
for %%i in (%my_arr%) do (
set /a count+=1
echo ## Count=!count!, content is %%i
if !count! EQU 1 (
set cube_name=%%i
)
if !count! GTR 1 (
echo ## Setting dim_arr to "%%i:#=,%"
set dim_arr=%%i:#=,%
echo
)
)
for %%i in (%dim_arr%) do (
echo the value of dim_arr is "%%i"
)
echo cube_name is "!cube_name!"
Output is
### show the variable(s) beginning with my_arr...
my_arr[1]=Mar2012
my_arr[2]=dim1,dim2,dim3,dim4
Part 2----
The value of my_arr is ""
cube_name is ""
As you can see your part2 fails completly.

Resources