I am trying to call an external program using Process:
Dim strExe As String = \"E:\\Projects\\Common Files\\mktorrent.exe\"
Dim p As New Process
Be simple:
Process.Start("c:\Your exe file", """" & "string with space" & """")
It's really an old - but unsolved - problem. My 2 cents of contribution.
Use CHR(34) before-and-after the string, delimiting it like:
Arg = "Name=" & chr(34) & "John Doe da Silva" & chr(34)
Just it!
Windows does not provide a common way of keeping arguments with spaces as single arguments. However there are a number of relatively common standards that you've tried.
So it comes down to either determining what argument processing mktorrent.exe
uses or, as you're trying to pass a filename, using "MSDOS" 8.3 format for the path which will have no spaces.
For the latter, this answer points to the Win32API GetShortPathName
.
Of course, 8.3 filenames can be disabled with modern Windows (all Windows NT-based systems I believe -- not that it often is). So your only full solution is to determine what argument processing mktorrent
supplies.
Since your comment suggesting the quotes are not being passed through I confirmed I see 'testing' 'testing' '1 2 3'
in the MsgBox
output of this vbscript:
Option Explicit
Dim arg
Dim line
For Each arg in WScript.Arguments
line = line & " '" & arg & "'"
Next
MsgBox Trim(line)
when executed using:
Dim strExe As String = "C:\Windows\System32\wscript.exe"
Dim p As New Process
Dim pinfo As New ProcessStartInfo
pinfo.UseShellExecute = False
pinfo.RedirectStandardOutput = True
pinfo.Arguments = " G:\Utils\Arguments.vbs testing ""testing"" ""1 2 3"""
pinfo.FileName = strExe
pinfo.WorkingDirectory = "G:\Utils"
pinfo.WindowStyle = ProcessWindowStyle.Normal
pinfo.CreateNoWindow = True
p.StartInfo = pinfo
p.Start()
So wscript
is seeing the quotes and is accumulating three arguments for the script.
BTW I just noticed your example attempts at getting quotes around the filename modify the fn
variable. Did you cater for this with the .WorkingDirectory
line, which should be using the unmodified filename?
This allows me to pass spaces to cmd. Hours of research turned up nothing; this thread came up constantly, hopefully this will help someone else.
Dim startprgm As New ProcessStartInfo("cmd.exe", "/C """"C:\Program Files (x86)\Folder\File""""" + strArguments)
note that the 4 double quotes lead the path, this part is important. leading the argument (/C) with 5 quotes doesn't work, but the trailing five can be divided into 4 and 1; and structured as such:
Dim startprgm As New ProcessStartInfo("cmd.exe", "/C """"C:\Program Files (x86)""""\Folder\File" + strArguments)
If you open cmd.exe and just send a command, you just need the first quote on the path (it doesn't need to be closed) but VB needs the trailing ones to "close" the quotes out.
best of luck, guys.
Please check the below link, its in C#, may be its helpful to you
Word command-line-arguments space issues
This WORKS:
Dim current_path, current_rulename, cmd1 as STRING
current_path = "C:\this folder\file name.exe"
current_rulename = "file name.exe"
cmd1 = "netsh advfirewall firewall add rule name = """ + current_rulename + """ dir = in action = block program = """ + current_path + """"
cmd1 &= " & "
cmd1 &= "netsh advfirewall firewall add rule name = """ + current_rulename + """ dir = out action = block program = """ + current_path + """"
cmd1 &= " & pause"
Process.Start("cmd", "/c " + cmd1)
Basically, the variables with spaces need to be enclosed like this:
""" + string_with_spaces + """
Broken into parts:
cmd1 =
"
netsh advfirewall firewall add rule name =
""" + current_rulename + """
dir=in action=block
program=
""" + current_path + """
"
This code joins two separate commands that use STRINGS with spaces.