Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access an element from a list/array in a windows batch code?

@echo off
set Counter=0
set folders=(14370437 14707356 16048938 16818856)

for %%f in (*.jpg) do call :p "%%f"

goto :eof

:p
    set /a Counter+=1
    set /a X=Counter %% 6

    %name% = folders[Counter] ???

    mkdir C:\output\%name%
    if %X%==1 copy %1 C:\output\%name%\front-image.jpg
goto :eof

I have a static list of folder names (the list is much longer than this example) and another function where I need to use them. I loop through a bunch of .jpg files and need to copy each one to the next folder in the list (also create the folder)

I couldn't find a way to retrieve an element from the list (or array?) folders, by index.

like image 776
Alexandru Pupsa Avatar asked Oct 21 '25 22:10

Alexandru Pupsa


1 Answers

batch can't work with lists or arrays, but you can simulate an array:

@echo off
setlocal enabledelayedexpansion
set folders=(14370437 14707356 16048938 16818856)
set i=0
for %%i in %folders% do (
  set /a i+=1
  set element[!i!]=%%i
)
set element
echo %element[2]%

Although this works, I strongly suggest, using the parantheses where they really belong:

@echo off
setlocal enabledelayedexpansion
set folders=14370437 14707356 16048938 16818856
set i=0
for %%i in (%folders%) do (
  set /a i+=1
  set element[!i!]=%%i
)
set element
echo %element[2]%

set number=3
echo !element[%number%]!
like image 185
Stephan Avatar answered Oct 25 '25 07:10

Stephan