如何在golang中为exec.command添加空格

huangapple go评论84阅读模式
英文:

How do you add spaces to exec.command in golang

问题

如何使用exec.command启动带有空格的文件?添加引号似乎不起作用,使用“或%20代替空格也不起作用。

package main

import (
	"fmt"
	"os/exec"
)

func main() {
	StartProcess("C:\\File With Spaces.txt")
}

func StartProcess(Filename string) {
	Filename = "\"" + Filename + "\""
	cmd := exec.Command("cmd", "/C", "start", Filename)
	err := cmd.Start()
	if err != nil {
		fmt.Println(err)
	}
}

希望这对你有帮助!

英文:

How do I use exec.command to start a file with spaces? Adding quotes doesn't seem to work, neither does " or %20 instead of the spaces.

package main

import (
"fmt"
"os/exec"
)

func main() {
    StartProcess("C:\\File With Spaces.txt")
}

func StartProcess(Filename string) {
    Filename = "\"" + Filename + "\""
    cmd := exec.Command("cmd","/C start",Filename)
    err := cmd.Start()
    if err!=nil{
    fmt.Println(err)
	}
}

答案1

得分: 1

如果你想在Windows上运行一个可执行文件,你不需要使用命令提示符。你只需要将可执行文件的路径传递给shell,它就会执行。

例如:

func StartProcess(Filename string) {
    // Filename = "cmd /C start \"" + Filename + "\""
    cmd := exec.Command(Filename)
    err := cmd.Start()
    if err != nil {
        fmt.Println(err)
    }
}

StartProcess("C:\\path to executable\\executable.exe")

话虽如此,通常在Windows上使用所有框架启动可执行文件时(包括Go语言),当尝试连接自己的参数时会遇到麻烦。这就是为什么exec.Commandargs参数是可变参数的原因。所以你的原始代码应该是这样的:

cmd := exec.Command("cmd", "/C", "start", Filename)
//                         ^^^^^^^^^^^^^^^ 可变参数使Windows正常工作
英文:

If you're trying to run an executable on Windows then you don't need command prompt. You just need to pass in the executable path to the shell and it will fire.

E.g:

func StartProcess(Filename string) {
    // Filename = "cmd /C start \"" + Filename + "\"" 
    cmd := exec.Command(Filename)
    err := cmd.Start()
    if err!=nil{
    fmt.Println(err)
    }
}

StartProcess("C:\\path to executable\\executable.exe")

That said, generally with all frameworks on Windows that start executables (Go appears to be included in this) you get in trouble when trying to concatenate your own arguments. That is why the args argument for exec.Command is variadic. So your original should have been this:

cmd := exec.Command("cmd", "/C", "start", Filename)
//                         ^^^^^^^^^^^^^^^ variadic arguments make Windows happy

答案2

得分: 1

这段代码可以在Windows系统中运行:

cmd := exec.Command("rundll32.exe", "url.dll,FileProtocolHandler", Filename)

请注意,这是一段Go语言代码,用于在Windows系统中打开指定的文件。

英文:

This works, but only in windows

cmd := exec.Command("rundll32.exe", "url.dll,FileProtocolHandler", Filename)

答案3

得分: 0

你需要使用cmd.Wait来等待命令执行完成,并检查方法返回的任何错误。

package main

import (
	"bytes"
	"log"
	"os"
	"os/exec"
)

func main() {
	args := os.Args

	cmd := exec.Command(args[1], args[2:]...)
	var b bytes.Buffer
	cmd.Stdout = &b

	err := cmd.Start()
	if err != nil {
		log.Fatal(err)
	}
	if err := cmd.Wait(); err != nil {
		log.Fatal(err)
	}

	if _, err := os.Stdout.Write(b.Bytes()); err != nil {
		log.Fatal(err)
	}
}

请注意,这是一个Go语言的代码示例,用于执行命令并等待其完成。在执行命令之前,你需要将命令行参数传递给os.Args,然后使用exec.Command创建一个命令对象。通过设置cmd.Stdout来捕获命令的输出,并使用cmd.Start启动命令。然后,使用cmd.Wait等待命令执行完成,并检查是否有任何错误发生。最后,将捕获的输出写入标准输出。

英文:

you need to cmd.Wait and check for any errors returned by your methods

package main

import (
	"bytes"
	"log"
	"os"
	"os/exec"
)

func main() {
	args := os.Args

	cmd := exec.Command(args[1], args[2:]...)
	var b bytes.Buffer
	cmd.Stdout = &b

	err := cmd.Start()
	if err != nil {
		log.Fatal(err)
	}
	if err := cmd.Wait(); err != nil {
		log.Fatal(err)
	}

	if _, err := os.Stdout.Write(b.Bytes()); err != nil {
		log.Fatal(err)
	}
}

huangapple
  • 本文由 发表于 2014年9月3日 06:01:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/25633077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定