Reputation: 143
In Windows CMD batch loop I would like to use dynamic variables: list1, list2 and list3 where the digit 1-3 is dynamic (ie: list&i), but I am struggling:
setlocal enabledelayedexpansion enableextensions
SET threads=3
set i=1
for /R %%x in (*.jpg) do ( call set LISTNAME=LIST!i! & SET LIST!i!=!LISTNAME! "%%x" & set /A i=!i!+1 & if !i! gtr %threads% (set i=1))
echo "first" %LIST1%
echo "second" %LIST2%
echo "third" %LIST3%
The exact spot where I struggle is:
SET LIST!i!=!LISTNAME! "%%x"
where I would like this for instance to be:
SET LIST1=!LIST1! "%%x"
However listname just translates to a string LIST1, never the variable LIST1. I also tried with no success:
SET LIST!i!=!LIST!i!! "%%x"
Purpose of the script: put the JPG file names in 3 lists EDIT to answer to first comment: files names distributed in round robin and separated with a space.
Upvotes: 2
Views: 1031
Reputation:
Assuming a round robin distribution you can easily get the list number with a modulus calculation. For demonstration purposes I only output filename.ext without drive:path.
@Echo off
setlocal enabledelayedexpansion enableextensions
Set /A "threads=3,i=0"
:: initialize List
For /L %%n in (1,1,%threads%) Do Set "List%%n="
for /R %%x in (*.jpg) do (
Set /A "n=i %% threads + 1,i+=1"
for %%n in (!n!) do Set "List%%n=!List%%n! %%~nxx"
rem call set "LIST!n!=%%LIST!n!%% %%~nxx"
)
echo "first " %LIST1%
echo "second" %LIST2%
echo "third " %LIST3%
Sample Output
"first " watch_dogs1.jpg watch_dogs4.jpg watch_dogs7.jpg
"second" watch_dogs2.jpg watch_dogs5.jpg watch_dogs8.jpg
"third " watch_dogs3.jpg watch_dogs6.jpg watch_dogs9.jpg
EDIT Inserted the for variable type of delayed expansion.
Upvotes: 7