2

I want to define a big array (>400 keys) in batch, but when I execute my script the windows close. I use this setting:

set FILE_LIST=(filename1.xxx [...] filename450.yyy)

Some help? Thx

Jack NUMBER
  • 446
  • 1
  • 5
  • 22
  • seems like you might be a limit, check out: http://blogs.msdn.com/b/oldnewthing/archive/2010/02/03/9957320.aspx – Alex Jun 26 '14 at 16:45

3 Answers3

4

A Windows Batch file have a limit in the value of each variable to 8192 characters, including the name of the variable and the equal sign. If the value of each "filename#.xxx " have 16 characters, you may store up to 8192/16=512 file names in one variable; to do that, you must use Batch commands. For example:

@echo off
setlocal EnableDelayedExpansion

set "FILE_LIST="
for /L %%i in (1,1,450) do set "FILE_LIST=!FILE_LIST!filename%%i.xxx "
echo FILE_LIST=%FILE_LIST%

Please, note that previous variable is a list, NOT and array. To define an array, use this method:

@echo off
setlocal EnableDelayedExpansion

for /L %%i in (1,1,450) do set "FILE_ARRAY[%%i]=filename%%i.xxx"
echo FILE_ARRAY:
set FILE_ARRAY

There is a limit of 64 MegaBytes for the total space occupied by all variables.

For a detailed description of arrays and other data structures in Batch files, see: Arrays, linked lists and other data structures in cmd.exe Batch script

EDIT: Reply to the comments

The Batch file below assume that there is one file name per line in the .txt file, and that file names does not include exclamation marks:

@echo off
setlocal EnableDelayedExpansion

rem Load the .txt file in FILE_ARRAY elements:
set num=0
for /F "delims=" %%a in (fileList.txt) do (
   set /A num+=1
   set "FILE_ARRAY[!num!]=%%a"
)

rem Process the FILE_ARRAY elements:
for /L %%i in (1,1,%num%) do echo Processing: %%i- "!FILE_ARRAY[%%i]!"
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • According to MS, `Starting with Windows Vista and Windows Server 2008, there is no technical limitation on the size of the environment block.` (from [here](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682653%28v=vs.85%29.aspx)) Can you please provide a link documenting/discussing the limit you are indicating? – MC ND Jun 26 '14 at 19:39
  • From the same link you posted: `However, there are practical limits depending on the mechanism used to access the block. For example, a batch file cannot set a variable that is longer than the maximum command line length.` – Aacini Jun 26 '14 at 19:46
  • Sorry, i was not clear. I was refering to the 64 MB limit. I am unable to find any technical reference/documentation about it. – MC ND Jun 26 '14 at 19:57
  • 1
    @MCND: From [this link](http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true): `The maximum individual environment variable size is 8192bytes. The maximum total environment variable size for all variables, which includes variable names and the equal sign, is 65,536KB`. – Aacini Jun 26 '14 at 20:31
  • Thank you. I was not aware of this limit. – MC ND Jun 27 '14 at 07:04
  • Sorry Aacini but I dont want to fill the array with a loop. They are differents files names, without incrementation. – Jack NUMBER Jul 02 '14 at 14:03
  • I put all files names in a .txt file and its size is 8.6KB – Jack NUMBER Jul 02 '14 at 14:04
  • Have the .txt file one name per line? May the file names have exclamation marks? – Aacini Jul 02 '14 at 22:09
  • I already think about external file but I choose to define all files names in the batch file because it's more portable. – Jack NUMBER Jul 08 '14 at 14:56
1

I finaly use this way:

set FILE_ARRAY[0]=filename1.xxx
set FILE_ARRAY[1]=filename2.yyy
set FILE_ARRAY[2]=filename3.zzz

for /F "tokens=2 delims==" %%i in ('set FILE_ARRAY[') do (
  echo %%i
)

Thanks for your answers.

Jack NUMBER
  • 446
  • 1
  • 5
  • 22
0

Seems like you might be hitting a batch file limitation (link), The following link describes a WA for this issue as dumping what you want in a var into a file, then reading that file back in when you need that massive var.

Ah batch, and your endless workarounds...

Alex
  • 917
  • 5
  • 11