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
Dim pinfo As New ProcessStartInfo
pinfo.UseShellExecute = False
pinfo.RedirectStandardOutput = True
pinfo.Arguments = " -a http://blah.com/announce.php -l " & FileSizeMarker & " " & fn
pinfo.FileName = strExe
pinfo.WorkingDirectory = fn.Substring(0, fn.LastIndexOf("\"))
pinfo.WindowStyle = ProcessWindowStyle.Normal
pinfo.CreateNoWindow = True
p.StartInfo = pinfo
p.Start()
The problem is with the filename (variable fn above). If it has spaces, the command chokes - without spaces, it works fine. I have tried adding 1, 2 or3 quotes, like this:
fn = Chr(34) & Chr(34) & Chr(34) & fn & Chr(34) & Chr(34) & Chr(34)
and also
fn = "\") & Chr(34) & fn & "\"& Chr(34)
and many other combinations, but it still gives me an error. Any thoughts on how I can get this to work? TIA
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.
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:
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
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 theMsgBox
output of this vbscript:when executed using:
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 WORKS:
Basically, the variables with spaces need to be enclosed like this:
Broken into parts:
This code joins two separate commands that use STRINGS with spaces.
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!
Be simple: