3

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

Chiwda
  • 1,233
  • 7
  • 30
  • 52
  • P.S. I have also tried single quotes... – Chiwda Oct 27 '12 at 09:04
  • I was forced to solve this problem by replacing the spaces with "_" (underscore). But because directory names can have spaces too, I had to first separate the file name from the directory name, rename the file with "_" (underscore) instead of " ", add back the directory name and then run the mktorrent. And this worked because I was changing the working directory to the file that I was torrenting, so the path became irrelevant. What a dumb hack! :-( – Chiwda Nov 19 '12 at 12:20

6 Answers6

2

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!

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

Please check the below link, its in C#, may be its helpful to you

Word command-line-arguments space issues

Community
  • 1
  • 1
andy
  • 5,979
  • 2
  • 27
  • 49
1

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?

Community
  • 1
  • 1
Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
  • Assuming it is the same version of the same program, note the example [here](http://wiki.bytesized-hosting.com/wiki/index.php/Mktorrent) removes the current directory from the filename (which still has spaces of its own). – Mark Hurd Oct 30 '12 at 06:49
  • Too bad there is no solution to such a simple problem. Basically, being able to pass quotes to the command line through the Process method – Chiwda Nov 01 '12 at 15:53
1

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.

KranDes
  • 11
  • 1
1

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.

0

Be simple:

Process.Start("c:\Your exe file", """" & "string with space" & """")
jcq
  • 167
  • 2
  • 13