Home > Back-end >  Pass many array elements as individual arguments to executable in Powershell
Pass many array elements as individual arguments to executable in Powershell

Time:10-23

I am trying to pass a list of paths as arguments to a binary. I have retrieved the files using

$files = (Get-ChildItem .\folder).FullName

and I want to pass them to my binary as following:

.\bin.exe $files[0] $files[1] ...

In other words, I want to pass each array element as a separate argument to the executable. In my case there are also many elements in the array: about 100000.

I've tried the following method:

.\bin.exe $files

And it seems to work for a small number of elements (about 400 absolute paths). Then Powershell throws the following error:

ResourceUnavailable: Program 'xml-to-graph.exe' failed to run: The filename or extension is too long.At line:1 char:1
  .\xml-to-graph.exe -output-dir fixtures -format "%n %m`n%.2FM`n%a`n"  …
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.

How can I achieve this?

CodePudding user response:

In my case there are also many elements in the array: about 100000.

That's going to be difficult. "Passing to a binary" means "building a command line and executing it", and command lines have a length limit (on Windows it's 8 kB, see MSDN).

You can see that you're hitting that limit in the error you receive ("The filename or extension is too long.").

How can I achieve this?

There is no way to circumvent the length limit. You will have to either

  • check the documentation of the xml-to-graph.exe program to see whether it supports alternative ways of getting the input files, or
  • do your work in batches that stay under the limit, if that's possible

xml-to-graph.exe seems to be an XML conversion program that takes many XML files as input and produces one output file. Depending on how this program works, there might also be the third option of joining all the small XML files into a single big one, and then calling xml-to-graph.exe joined.xml.

  • Related