How many variable can I use in Batch File

2019-05-07 09:35发布

问题:

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!

回答1:

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):

  • Your computers architecture is 64-bit (preferably Itanium class).
  • You have a 128 TB hard drive with NTFS
  • You have a streamlined 32 core 4THz (terahertz) processor so you can see it complete before the day you die of old age.
  • You have at least and at least 384GB of RAM (if you compressed the names and used empty strings as values)
  • There are only the 65807 files that initially come with windows 7 professional with sp1 plus your 1 batch file on your hard disk.
  • There are only the 96783 registry indexes that initially come with windows 7 professional with sp1 on your hard disk.

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)