1

I'm new to this forum, so please correct me if I'm asking this the wrong way or not specific enough.. While coding in VB.NET I'm trying to pass multiple commands, and an argument containing a reference to a path:

Dim p As New Process
Dim pi As New ProcessStartInfo
pi.Arguments = " " & "/K """ & "C:\program files\gdal\gdalshell.bat" & """ & " & "cd C:\program files\gdal" & _
         " & " + "gdal_translate" + " -of Jpeg -outsize 2000 2000 """ & "D:\box sync\my box (907070)\RIS_RHDHV_Overgang\GDAL\test2.xml" & """ "
pi.FileName = "C:\windows\syswow64\cmd.exe"

p.StartInfo = pi
p.Start()

The command prompt returns: 'C:\Program' is not recognized as an internal or external command, operable program or batch file.

I did some research on the matter and found: vb.net How to pass a string with spaces to the command line

This however still doesn't seem to solve the problem. When I execute the following code, it runs without issues:

Dim p As New Process
Dim pi As New ProcessStartInfo
pi.Arguments = " " & "/K """ & "C:\program files\gdal\gdalshell.bat" & """ & " & "cd C:\program files\gdal" & _
         " & " + "gdal_translate"
pi.FileName = "C:\windows\syswow64\cmd.exe"

p.StartInfo = pi
p.Start()

To me it looks like the problem is caused by the path reference inside an argument. I have read and used the different answers for using multiple commands, without any luck.

It would be great if someone could help me on this topic.

Kind regards,

Stuart

Community
  • 1
  • 1
S. Kerkhof
  • 13
  • 3

1 Answers1

0

First of all, as a little note you don't need to add a space in the beginning of the arguments. That's only done when you write the entire command (including the executable) in one line.

Now, the correct way to pass a path as an argument is to surround it with quotes. So something like this should do:

(please note that I have also shortened some unnecessary concatenations, etc.)

pi.Arguments = "/K """"C:\program files\gdal\gdalshell.bat"" & cd ""C:\program files\gdal"" & " & _
         "gdal_translate -of Jpeg -outsize 2000 2000 ""D:\box sync\my box (907070)\RIS_RHDHV_Overgang\GDAL\test2.xml"""""

EDIT:

I found that you also have to put the entire text after /K in quotes for it to work.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • Thanks for your response. This however still doesn't seem to work.. with same errors as before. Also, I believe the "cd C:\... etc" doesn't need to be quoted, as it wouldn't have to be quoted when typing directly into the cmd prompt – S. Kerkhof Feb 06 '17 at 22:51
  • @S.Kerkhof : You don't need to quote it when you only type a single command, no, but here you are executing multiple commands and parameters (separated by spaces, and there are spaces in the path). It wouldn't know how to parse the full path if it weren't for the quotes, so those are necessary. – Visual Vincent Feb 06 '17 at 23:15
  • I can't see why it wouldn't work through, and unfortunately as I'm no longer in a position to test this I'm afraid you'll have to wait until tomorrow before I can check. – Visual Vincent Feb 06 '17 at 23:20
  • Hi Vincent, I also can't see why the solution wouldn't work. When I remove the ""D:\box sync... etc"" path from the last argument, it does work. Also, this is a class library project which I run in autocad application. (I don't know if that matters). Also, when I try to put the arguments in a stand alone command prompt (without script), it does work. – S. Kerkhof Feb 07 '17 at 20:42
  • @S.Kerkhof : Sorry for the delay... After testing this I found out that you also have to put all the text after `/K` in quotes for it to work. I have updated my answer's code. – Visual Vincent Feb 08 '17 at 22:05
  • Thank you for your response. That seems to do the trick... I thank you very much for your help! I have the feeling that I'm missing some (basic) knowledge in these topics. Is there a place where I can learn about these kind of interactions with VB.NET/ command prompt? – S. Kerkhof Feb 11 '17 at 23:07
  • @S.Kerkhof : Not really, no... This isn't actually related to VB.NET, but Windows in general. The same rules apply for the entire operating system, i.e. if you'd execute it from Windows's `Run` prompt you'd have to put the text after `/K` in quotes there aswell. – Visual Vincent Feb 12 '17 at 01:20
  • .NET Framework's methods and classes are actually just wrappers of the WinAPI. In this case `Process.Start()` is a wrapper of [**`ShellExecute()`**](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx) or [**`CreateProcess()`**](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx) (depending on the settings). – Visual Vincent Feb 12 '17 at 01:24