4

When running long command in .bat file (say 300 characters length)

for example:

Some_exe "C:/Documents and Settings/Some user/Some folder1/Some folder2/Some folder3/Some folder4 ... -Some_exe_arg1="arg 1 name" -Some_exe_arg2="arg 2 name" -Some_exe_arg3="arg 3 name"  

Is there a limit on the line size CMD.exe can process? Should i use .CMD or .BAT? Is there any way i can overcome this limitation?

Thank you!

jeb
  • 78,592
  • 17
  • 171
  • 225
JavaSheriff
  • 7,074
  • 20
  • 89
  • 159

2 Answers2

6

All versions of Windows from XP onward support a maximum batch line length of 8191 bytes: http://support.microsoft.com/kb/830473

Often times executables get around the command line length limit by allowing for parameter values to be specified in a file. For example, FINDSTR has a /G:filename option that specifies the name of a file that contains search strings.

There is no difference between .BAT vs .CMD with regard to line length. In fact, there is almost no difference between them at all: https://stackoverflow.com/a/148991/1012053. (Note - most of the comments questioning the accuracy of the linked answer predate the most recent edited version of the answer. The linked answer is now correct.)

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
6

The minimum of the maximum batch line length is 8191 bytes!

This means that a line can be in any case 8191 bytes long, but it is also possible to create legal batch lines with nearly unlimited length.

Samples

echo Longline with 8191 characters.........

set "var=a"
echo UltraLongLine %var:4000chars=% %var:4000chars=% %var:4000chars=% %var:4000chars=%

echo Test <8000Chars <8000chars <8000chars .... <nul

The point is here, that all lines are less than 8192 bytes long after parsing

jeb
  • 78,592
  • 17
  • 171
  • 225
  • 3
    An even simpler valid long line is 20,000 spaces followed by `echo test`. Technically you are correct, the line length can exceed 8191 characters. But I don't see how this is helpful or in the spirit of the original question. I suppose there might be some scenario where the trick could be useful. But when I read the question, I assume it is the post parsing/functional line length that the OP is interested in. – dbenham Mar 08 '12 at 15:54