Re: [go-nuts] Problems with os/exec under Windows - arguments / parameters supplied with double quotes

1,815 views
Skip to first unread message

Jan Mercl

unread,
Sep 1, 2012, 2:47:39 AM9/1/12
to Schasologe, golan...@googlegroups.com
On Fri, Aug 31, 2012 at 11:40 AM, Schasologe <e...@living-lexicon.at> wrote:
> I have the following problem:
> I try to execute an external program with os/exec under Windows (Windows 7,
> 32 bit). I use this code:
>
> qvParam = " /R /NOSECURITY "
> ......
> parameter := qvParam + filename
> cmd := exec.Command("Qv.exe", parameter)
> err := cmd.Start()
> filename is read in from a textfile.
>
> When I run the code, "Qv.exe" is being launched, but NOT with the correct
> parameters. If I check what is happening with procmon, I see that Windows
> gets this command:
> Qv.exe " /R /NOSECURITY D:\lager.qvw"
>
> (including the double quotes!!). If I paste this command string to the
> command line it fails of course, becaus the command should look like this
>
> Qv.exe /R /NOSECURITY D:\lager.qvw
>
> I have been trying for hours now, also with back quotes and concatenation of
> "Qv.exe" + parameter + filename, but with no success; I always get the
> double quotes in the parameters.
>
> Can anybody help? I've already searched the internet but haven't found a
> solution to my problem.
> BTW I'm new to Go.

You're carefully constructing and passing a single parameter to qv.exe
and that's what you get. It's correct per the documentation.

I think something like 'cmd := exec.Command("qv.exe", "/R",
"/NOSECURITY", filename)' should work. No windows, not tested.

-j

DisposaBoy

unread,
Sep 1, 2012, 6:19:17 AM9/1/12
to golan...@googlegroups.com


On Friday, August 31, 2012 10:40:47 AM UTC+1, Schasologe wrote:
I have the following problem:
I try to execute an external program with os/exec under Windows (Windows 7, 32 bit). I use this code:
 
qvParam   = " /R /NOSECURITY "
...... 
parameter := qvParam + filename
 cmd := exec.Command("Qv.exe", parameter)
 err := cmd.Start()
filename is read in from a textfile.
 
When I run the code, "Qv.exe" is being launched, but NOT with the correct parameters. If I check what is happening with procmon, I see that Windows gets this command:
Qv.exe " /R /NOSECURITY D:\lager.qvw"
 
(including the double quotes!!). If I paste this command string to the command line it fails of course, becaus the command should look like this
 
Qv.exe /R /NOSECURITY D:\lager.qvw
 
I have been trying for hours now, also with back quotes and concatenation of "Qv.exe" + parameter + filename, but with no success; I always get the double quotes in the parameters.
 
Can anybody help? I've already searched the internet but haven't found a solution to my problem.
BTW I'm new to Go.
 
Rgds and thanks in advance,
Joachim

 

Other comment has already said what I think is the problem but I think you should keep in mind that you're not dealing with the shell(cmd.exe) as you would be in some other languages so a string `-a b c.d` is one argument although it'd be 3 when run from the command line


 

brainman

unread,
Dec 11, 2012, 1:40:47 AM12/11/12
to golan...@googlegroups.com, harvey....@gmail.com
On Tuesday, 11 December 2012 16:44:40 UTC+11, harvey....@gmail.com wrote:

> But in a Go program using a Command, you just need to ensure that each argument is passed separately - no quotes are required. It seems like it won't work, but it does.
> In fact if you add quotes to the command, it will fail and return an error.

Here are the rules that we followed http://code.google.com/p/go/source/browse/src/pkg/syscall/exec_windows.go#17. We tried our best to do what Microsoft doco suggests.

> quote := string('"')
> ... quote+"File 1.txt"+quote, ...

You could just use "back quotes" instead, like `"File 1.txt"`. See http://golang.org/ref/spec#String_literals for details.

Alex

André Moraes

unread,
Dec 11, 2012, 5:29:51 AM12/11/12
to harvey....@gmail.com, golan...@googlegroups.com
> func main() {
> quote := string('"')
> // this fails ...
> cmd := exec.Command("cmd.exe", "/c", "copy", quote+"File 1.txt"+quote,
> quote+"File 1 (copy).txt"+quote)

This is because you are telling to the command, that the name of the
file is QUOTEFile1 1.txtQUOTE, when in fact, the file name is File 1.

You will need to escape the whitespace using quotes only if you are
concatenating the filename with another thing and then passing it to
the command. For example:

cmd := exe.Command("cmd.exe", "/c", "copy", fmt.Sprintf(`input="%v"`,
"File 1.txt"))

--
André Moraes
http://amoraes.info
Reply all
Reply to author
Forward
0 new messages