1

I'm running some code in C# where I call a batch file, and I need to pass a Path with space as an argument and it does not work.

I have tried to call my argument different ways in the batch %1 , %~1, "%1", '%1'. None of these work. Also in my C# code I tried to convert it to string and it wont work either

C# code.

string argument =   textBox10.Text.ToString()  ; 
string command = @"/c powershell -executionpolicy unrestricted X:\PathToBatch\Run.bat" + " " + argument;
System.Diagnostics.Process.Start("cmd.exe", command);

Batch code :

echo %1
Pause

When I pass an argument C:\Program Files\Test as a directory, it prints "C:\Program" and stops at the space. How can I get the full path ?

Sebastian Siemens
  • 2,302
  • 1
  • 17
  • 24
Rhon Yz
  • 145
  • 1
  • 1
  • 8
  • What have you tried so far? You have to use Quotes like this `+ " """ + argument + """";` Did not actually tested this. Just for explanation. – Sebastian Siemens May 23 '19 at 08:50
  • 1
    what happens when you do `echo "%*"` – Gerhard May 23 '19 at 08:51
  • when I go %* it shows everything but the fact is that I have 6 arg, so I might not be able to use that. Im my command line it looks like that : @"/c powershell -executionpolicy unrestricted X:\PathToBatch\Run.bat" + " " + arg1 + " " + arg2 + " " + arg3 – Rhon Yz May 23 '19 at 09:00
  • I already tried to use quotes too yeah – Rhon Yz May 23 '19 at 09:01
  • you should pass your arguments quoted from `C#` code – spirit May 23 '19 at 09:03
  • The solution can vary based on what command you want to execute, so please make your question more clear and exact. – shingo May 23 '19 at 09:11
  • Yes, I know, I am showing you your problem. `cmd` works on next command based on spaces, so you would need to let it know which stdin is which position.. i.e. `echo %~1 %~2` and run it as `batchfile.cmd "c:\program files" stdin2` compared to `batchfile.cmd c:\program files stdin2` (which now has 3 args) – Gerhard May 23 '19 at 09:21
  • 2
    Why do you use powershell and not run the batch file directly? Introducing powershell as a middleman makes matters more complicated – NineBerry May 23 '19 at 09:24

3 Answers3

1

Try this :

string command = @"/c powershell -executionpolicy unrestricted X:\PathToBatch\Run.bat"+ " \""+ argument +"\" ";
System.Diagnostics.Process.Start("cmd.exe", command);

This will "write" the following line in your console :

/c powershell -executionpolicy unrestricted X:\PathToBatch\Run.bat "C:\Program Files\Test"
spirit
  • 3,265
  • 2
  • 14
  • 29
Le Pain
  • 75
  • 9
  • Are still you having the same issue with only "C:\Program" being printed ? – Le Pain May 23 '19 at 09:07
  • Yes, also when I have several args the next ones take the value 'Files\Test' – Rhon Yz May 23 '19 at 09:16
  • I'm not a batch expert but by looking on the web the only thing that could cause this to happend is to forget the double quotes on the command line. Try to write it manually in the cmd like so : `/c powershell -executionpolicy unrestricted X:\PathToBatch\Run.bat "C:\Program Files\Test"` If you still have the same results then there is something wrong with batch. (or with me but in any case i don't think i can help you anymore atm ) – Le Pain May 23 '19 at 09:25
0

pass your argument double quoted, like so: "C:\Program Files\Test".

the issue is that your argument contains a space so it treated as two different arguments

spirit
  • 3,265
  • 2
  • 14
  • 29
  • @Le Pain, purposed a solution for your code. my point is if you want to pass one argument which contains spaces then you must quote it, so the shell will also treat it as one argument – spirit May 23 '19 at 09:01
  • still have the same issue even when it is quoted – Rhon Yz May 23 '19 at 09:11
  • @RhonYz check this question: https://stackoverflow.com/questions/5510343/escape-command-line-arguments-in-c-sharp it might help you – spirit May 23 '19 at 09:32
0

It worked with simples quotes " '" + arg + "' ", and by calling it in the batch as %~1

Rhon Yz
  • 145
  • 1
  • 1
  • 8