3
@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.

Alexandru Pupsa
  • 1,798
  • 4
  • 21
  • 40

2 Answers2

6

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%]!
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you! This works just as expected. You saved my day :) – Alexandru Pupsa Jan 27 '16 at 16:54
  • @Stephan: If "`batch` can't work with arrays", how is possible that your code correctly process the array elements? Perhaps "simulate an array" is a phrase that makes no sense... See: http://stackoverflow.com/questions/10544646/dir-output-into-bat-array/10569981#10569981 – Aacini Jan 27 '16 at 17:12
  • 2
    @Aacini: a bunch of independent variables doesn't make an array in my opinion. An Array is a single object, not a collection of similar named variables - even if the syntax of addressing the "elements" is similar. – Stephan Jan 27 '16 at 17:19
  • Well, accordingly to your opinion, arrays, structures, lists, etc., can NOT really exist in the computer either, because the RAM is just a bunch of bytes! All these concepts are "simulated" by the compilers and interpreters of high-level programming languages, right? (did you read my linked post?) – Aacini Jan 27 '16 at 18:43
  • yes, I did read your post. But do compilers really exist? Do computers really exist? Or are they "simulated" by a big bunch of atoms where each atom is simulated by some strings? It's just where you draw the line. My personal line for arrays is: "if it's not handled as a single object (by compiler/interpreter/whatever), it's not an array". Just my opinion. – Stephan Jan 28 '16 at 08:29
  • Excuse me, my purpose is not to be annoying but to stimulate interesting discussions, ok? **`;-)`** In the "C" programming language and most "old" languages there is not a single feature that handle an array as single objet, excepting its declaration; all array processing is done via its individual elements. Are arrays in those languages real arrays or not? – Aacini Jan 28 '16 at 16:03
  • Don't know `C`. Can you do something like `newarray = oldarray`? If yes, I'd name it an Array. If you have to iterate through each element to get a copy of the whole construct, I'd say it's a - however you want to name it: pseudo-array, simulated array, bunch of variables with similar name, ... If you have a lot of single sheets of paper and call it a book, I wouldn't kill you and I would know, what you mean, but I'd consider it as not precise. I _would_ be annoyed, if you'd kill me, because I called your book a "bunch of sheets". But in the end it's just a matter of definition. – Stephan Jan 28 '16 at 16:31
  • @Aacini PS: not annoyed at all :) – Stephan Jan 28 '16 at 17:06
0

You may "shift" or "rotate" the elements in a list this way:

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

set "folders2=%folders%"
for %%f in (*.jpg) do call :p "%%f"

goto :eof

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

    for /F "tokens=1*" %%a in ("%folders2%") do (
       set "name=%%a"

       rem Use this for "shift elements"
       set "folders2=%%b"

       rem OR use this for "rotate elements"
       set "folders2=%%b %%a"
    )

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

For a complete description of how to manage an array in Batch files, see: Arrays, linked lists and other data structures in cmd.exe (batch) script

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108