2

Problem: In powershell 5.1 I run a command, myProgram, and pass to its -itemsToProcess flag a comma separated string list (of dynamic length), $commaSeparatedList, as arguments. There are often too many characters, sometimes 125000 characters, or more, or less, in $commaSeparatedList which can cause the error shown below.

$commaSeparatedList = 'file1,file2,file3,file4 ... fileX'

myProgram -itemsToProcess $commaSeparatedList

Question: How might I avoid the error? How might i split this into multiple calls such that the error is never thrown? The calls must be sequential too, not in parallel.

The pseudo code setup above works fine/succeeds when $commaSeparatedList is short in character length , however if its too long it crashes/fails/errors:

"the filename or extension is too long" 

enter image description here

For example, the dynamically generated $commaSeparatedList has been 125k characters and could potentially be longer or shorter.

how might we detect and avoid the error? Perhaps somehow split it into multiple myProgram -itemsToProcess calls to avoid the error? What would that look like?

mklement0
  • 382,024
  • 64
  • 607
  • 775
Peter Noges
  • 275
  • 2
  • 20
  • 2
    Deep [under the covers](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw), "The maximum length of this string is 32,767 characters", where "this string" is "The command line to be executed." You say you're building that parameter dynamically, so just don't build it to be bigger than that limit in a single invocation. – Lance U. Matthews Jan 08 '21 at 18:46

1 Answers1

1

Thanks for the comments, I split the input into smaller strings and made more calls

Peter Noges
  • 275
  • 2
  • 20