Batch Arrays

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Batch Arrays

Post by MigrationUser »

22 Mar 2013 16:12
TimaBVI

Hi everyone,

I came across a way to build arrays in batch.
In actual programming terms, it not an array but a dynamic creation of variables.
Here it is:

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R "%~1" %%A IN (.) DO (
SET var=%%A
SET var=!var:~0,-2!
FOR %%B IN ("!var!\*") DO (
CALL SET /A "cnt!cnt!=cnt!cnt! + 1")
CALL ECHO cnt%%cnt%% = %%cnt!cnt!%%
SET /A cnt=!cnt! + 1 )
ENDLOCAL
Here is the breakdown:
1. Take a directory as a parameter.
2. Get the next subdirectory of the directory.
3. Dynamically create a new variable to hold the total number of files in the current subdirectory.
4. For each file in the subdirectory increment the variable by 1.
5. Display the total number of files in the current subdirectory.
6. Repeat steps 2 to 5 until there are no more subdirectories.

Note: Steps 3 and 4 were done on one command line.

At the end of the run, cnt will contain the total number of files in subdir1,
cnt1 will contain the total number of files in subdir2 and so on.

----------------------------

#2 22 Mar 2013 19:19
Ocalabob

Greetings TimaBVI,
There maybe a problem with your code. I don't see a closing parenthesis
after the first DO nor do I see a second %%B.

Best wishes!

----------------------------

#3 22 Mar 2013 19:37
TimaBVI

Hi Ocalabob,

If you count the number of open and closed parenthesis, you'll find that all required parenthesis are there.
They are just in hard places.

Take a closer look.

Thanks

----------------------------

#4 24 Mar 2013 00:06
Ocalabob


TimaBVI,
I put my glasses on! My mistake :)

later.

----------------------------

#5 30 Apr 2013 06:45
Aacini


Hi TimaBVI, may I do some comments about your code?

- SET /A command may directly manage names of variables, so it is not necessary to expand the values of variables via a CALL. This is particularly useful when manage arrays.
- SET /A var+=1 is an easier way to increment a variable. Again, this is particularly useful when manage array elements.
- It is faster and clearer to use a FOR to expand the value of the subscript in an array element instead of a CALL.
- I suggest you to manage all array elements in the same way. Your code manage the first element as an individual variable and the rest as array elements.
- I suggest you to use the standard array notation to manage array elements enclosing the subscript in square brackets; it is clearer this way.

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R "%~1" %%A IN (.) DO (
   SET var=%%A
   SET /A cnt+=1
   FOR %%B IN ("!var:~0,-2!\*") DO (
      SET /A cnt[!cnt!]+=1
   )
   FOR %%c in (!cnt!) do ECHO cnt[%%c] = !cnt[%%c]!
)
ENDLOCAL
You may read a detailed discussion on this topic at this thread:
http://stackoverflow.com/questions/1016 ... 0#10167990

There is a thread in DosTips forum that also discuss how to manage other data aggregates in Batch files, like structures and linked lists: http://www.dostips.com/forum/viewtopic.php?f=3&t=3244

----------------------------

#6 30 Apr 2013 12:31
TimaBVI

Hi Aacini,

I have looked at the changes. That is great that you didn't have to use the CALL command
and I see that you placed the echo in a separate FOR loop. That I did not think about and I
see that you made the variable look like a regular array with the []. The batch script is just
an example, optimization was not implemented when I wrote it. But thanks for the adjustments.

Last edited by TimaBVI (30 Apr 2013 12:32)
Post Reply