1

I'm trying to pass a folder path via commandline argument to an application.

Problem: my folder path contains space " " in it's string. When I read the commandline arguments in the application I get my path chopped into pieces on the space " "

Sub Main()

    Dim arguments As String() = System.Environment.GetCommandLineArgs()
    For Each Arg As String In arguments
        Console.WriteLine("Argument : " & Arg)
    Next

    Console.ReadLine()

End Sub

Edit: added code to build my argument

Private Sub btn_Copy_Click(sender As Object, e As EventArgs) Handles btn_Copy.Click

    Dim args(3) As String
    args(0) = """" & tb_CopyFromPath.Text & """"
    args(1) = """" & tb_CopyToPath.Text & """"
    args(2) = """" & tb_ItemTag.Text & """"
    args(3) = """" & tb_Prefix.Text & """"

    Dim argument As String
    argument = args(0) & " " & args(1) & " " & args(2) & " " & args(3)

    Process.Start("J:\VB.NET - EM AddIn\EM_Design_AddIn\CopyDesign\bin\Debug\CopyDesign.exe", argument)
End Sub

This result isn't okay. The first argument of the first path now contains a piece of the second path.

Screenshot


Edit: add value result from debug.

"""C:\VaultWorkspace\cadcampc\03-Vessel configurator - R2.0\Nozzles\WN_RF_ASME_B16.5\"" ""C:\VaultWorkspace\cadcampc\03-Vessel configurator - R2.0\Test Copy Design\N03"" ""N3"" ""12345-3"""
Mech_Engineer
  • 535
  • 1
  • 19
  • 46
  • Could you provide value for `Dim argument As String argument = args(0) & " " & args(1) & " " & args(2) & " " & args(3)` while debugging? – user2216 Apr 19 '16 at 09:56
  • I have an idea :) Is there a chance that you entered to `tb_CopyFromPath.Text` value with double-quotes? If you enter to command line two double-quotes `""` it will be received like one. (It is the way to write double-quote in argument) – user2216 Apr 19 '16 at 09:59
  • I added my msgbox result, If I'm needed to debug, i will have to change some output settings to get things going. – Mech_Engineer Apr 19 '16 at 10:41
  • Okay, i Changed the result to the debug value, that doesn't look good. – Mech_Engineer Apr 19 '16 at 10:47
  • @Mech_Engineer There are too many double-quotes. Each argument should has just one pair of them. Result should be ""C:\VaultWorkspace\cadcampc\03-Vessel configurator - R2.0\Nozzles\WN_RF_ASME_B16.5\" "C:\VaultWorkspace\cadcampc\03-Vessel configurator - R2.0\Test Copy Design\N03" "N3" "12345-3"" – user2216 Apr 19 '16 at 11:14
  • Well, yes I can see that but how do you achieve this output? I'm totally confused.. – Mech_Engineer Apr 19 '16 at 11:22
  • I don't understand, if I add blockquotes either using `""""` or `chr(34)` it gives me double the amount of quotes required. When I add none nothing is added and my string is also invalid. – Mech_Engineer Apr 19 '16 at 11:50
  • After what executed line of code you receive double amount? – user2216 Apr 19 '16 at 12:32
  • As it is shown in my question. and suggested by @VisualVincent his solution – Mech_Engineer Apr 19 '16 at 12:42
  • If you create a new application to test creating a string with double quotes, does it still add too many of them? Also, what version of Visual Studio are you using (if you are using VS, that is)? – Visual Vincent Apr 19 '16 at 14:42
  • Yes, I already checked that. Same result, my version is 2015 community ( the free one ) – Mech_Engineer Apr 19 '16 at 14:50
  • Do you think you could create a small project sample for me/us? Make sure that the same problems occur. – Visual Vincent Apr 19 '16 at 20:24
  • I've created a small project and the problem doesn't occur. I was wrong in my previous statement. The output is correct in the new project. – Mech_Engineer Apr 20 '16 at 06:24
  • What happens if you start your application via cmd and pass arguments manually? – Visual Vincent Apr 22 '16 at 07:21

3 Answers3

3

It is very simple. Just use ". If you pass test test test parameters, you'll get 3 arguments. But if you write test "test test", you'll receive two parameters: test and test test.

user2216
  • 809
  • 1
  • 8
  • 24
1

Just pass it with double-quotes around it.

I.e:

app.exe "C:\Sub folder 1\Sub folder 2"

If you do it in code:

Process.Start("app.exe", """" & path & """")

The quotes specifies the start and end of an argument.


EDIT:

In your case you could do this instead:

argument = """" & args(0) & """  """ & args(1) & """ """ & args(2) & """ """ & args(3) & """"
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • Hi! thanks for the reply. I tried implementing your answer, but failed to do so. I edited the question with more details. Maybe you can see what i'm doing wrong? – Mech_Engineer Apr 19 '16 at 09:46
  • I suggest some corrections (add Trim): `Process.Start("app.exe", """" & path.Trim('""') & """")` – user2216 Apr 19 '16 at 10:00
  • @user2216 : Single quotes are not valid in that context, also if the path has been constructed properly with for example `Path.Combine()` it cannot contain any double quotes. – Visual Vincent Apr 19 '16 at 10:02
  • @VisualVincent Sorry, but I cannot agree. If you add one pair of double quote `"file path"` and then one more `""file path""`, you will get two arguments `file` and `path`. – user2216 Apr 19 '16 at 10:08
  • @user2216 : But `"file path"` is not a valid path, and `OpenFileDialog`s etc don't return with quotes. – Visual Vincent Apr 19 '16 at 10:16
  • @VisualVincent Yes, but before `argument = """" & args(0) & """"` there are double-quotes already after this line `args(0) = """" & tb_CopyFromPath.Text & """"` – user2216 Apr 19 '16 at 10:18
  • @user2216 : He was supposed to remove that. Anyway, he can use `Trim()` if he wants and I understand and respect your opinion, but I leave it as it is for now. – Visual Vincent Apr 19 '16 at 10:20
1

Use CHR(34) to delimiter a string with spaces within. See my answer here: vb.net How to pass a string with spaces to the command line

David BS
  • 1,822
  • 1
  • 19
  • 35