batch-filevariablescmd

How many variable can I use in Batch File


I am a little curious about how many variable I can set in Batch File.

I have a little script here

@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /L %%P IN (1,1,2147483647) DO (
    SET Foo[%%P]=%%P
    ECHO !Foo[%%P]!
)

But this script would take forever to run. So, is there any way to find out variable amount limit? (and of course how many variable can be used in Batch file.)

EDIT:

Since comment below says that var. Amount may vary due to different OS. So if you would answer, use Windows 7 64bit w/ 4 GB RAM as your guideline.

Any help will be appreciated!


Solution

  • Well the real answer is that it depends on the limitations of your machine such as RAM, especially hard disk space, and so forth. But, I am fairly certain you are looking for the theoretical answer, not the practical answer (I would be too!).

    So, for the theoretical answer, let's assume here the following (sorry if I sound sarcastic, but these are the real requirements and they're not so realistically possible):

    So, the big limiter we encounter is how the batch variables are stored. They are addressed in an array using signed ints. This allows for a maximum of 2,147,483,647 variables stored in the memory. Now, for where the hard disk comes into play. If you would allow the hard disk to be used for batch variables, then you will need 2 batch variables to use the hard disk files as batch variables: one variable for the cache, and one in the for loop. And, now we have 4,294,967,295 extra batch variables that can be utilized. Now for the registry, assuming windows uses signed integers to index the registry, that would allow there to be a total of 2,147,483,647 registry indexes. Now, to total it all up, (2,147,483,647 - 2) + (4,294,967,295 - (65807+1)) + (2,147,483,647 - 96783) =

    8,589,771,996

    (Assuming fair play without c++ coding cheatzies)