0

so I'm trying to send an array from my C# code to a batch file, on which I need to perform a for-each loop.

My C# code:

string MyBatchFile = {the batch file path}
int[] x = {1,2,3};
var process = new System.Diagnostics.Process
{
    StartInfo =
    {
       Arguments = String.Format("{0}", x)
    }
};
process.StartInfo.FileName = MyBatchFile;
process.Start();

My batch file:

set array=%1
FOR %%x IN %array% DO (
echo %%x
/*Some more lines here*/
)
pause

This doesnt seem to work, and if I print %array% I get "System.Int32[]". What am I doing wrong?

Note: The main goal is not to print the array, but rather to perform a few actions on each value in the array. The printing is merely an example.

EDIT: I managed to finally do it, found some workaround :) I won't be publishing how I did it, since it's a "duplicate", isn't it? Cheers.

Barak B
  • 84
  • 6
  • 1
    That's not the C# code that gives you `"System.Int32[]"`, that's what you get if you call `Int32[].ToString()`. Anyway if you have an array you want to print as a single string, look into `string.Join()` as explained in the duplicate. – CodeCaster Jun 19 '18 at 10:23
  • Hey, thanks for the reply. But, as I said, I need to perform a for-each loop, not just to print the array, the print was an example (and is just a small part of the code), that is why I wrote "/*some more lines here*/" . My question remained unanswered. – Barak B Jun 19 '18 at 10:38
  • Could you please remove the "duplicate" tag from this question, I believe it is misleading. @CodeCaster – Barak B Jun 19 '18 at 11:11
  • It's not misleading. Your C# code does not contain an integer array. Create a [mcve]. If it does contain an integer array, see the duplicate for how to print that as a comma-separated string. – CodeCaster Jun 19 '18 at 11:53
  • Added some changes to the code, and rephrased some parts of the question. So now to the point - I need to perform a for-each loop on an int array from within the batch file, ***I do not need to print it***. – Barak B Jun 19 '18 at 12:02
  • And now we're back to square one, and the duplicate answers your question. Your problem _is_ with printing, or rather formatting, namely formatting the int array into an argument string. It now prints the string "System.Int32". Do as the duplicate says. – CodeCaster Jun 19 '18 at 12:05
  • But how does it help me, to format the int array into a string like in the duplicate? Can I iterate a string? – Barak B Jun 19 '18 at 12:29
  • See the EDIT in the question @CodeCaster – Barak B Jun 19 '18 at 12:38
  • I've removed most of my comments. I'll admit that only half of your problem is answered by the duplicate. – CodeCaster Jun 19 '18 at 20:24

1 Answers1

0

You need a combination of joining strings, quoting command line arguments and parameter cleanup.

In order to build your argument string, you need to String.Join() your integer array into a single string:

new StartInfo
{
    Arguments = string.Format("\"{0}\"", string.Join(" ", x));
}

Now arguments holds the string "1 2 3" including quotes. This makes it a single argument to your batch file.

Then in the batch file, you need to clean the argument and then you can loop over it:

@ECHO OFF

REM Copy the argument into a variable
SET array=%1
REM Trim the quotes from the variable
SET array=%array:"=%

REM Loop over each space-separated number in the array
FOR %%A IN (%array%) DO (
    ECHO %%A
)

References:

You could also still go the comma-separated way, see How to loop through comma separated string in batch?.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • I was really just about to mark this question as answered, and thank you for your precious time and perhaps apologize too. But now it is marked as duplicate again. This is truly ridiculous. I won't be wasting any more time trying. Best of luck, and sorry if I offended you. – Barak B Jun 19 '18 at 13:58
  • No problem, it was a nice exercise and I hope I haven't offended you as well. – CodeCaster Jun 19 '18 at 14:00