执行命令失败,文件名包含两个空格。

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

Failed exec.Command with fileName contain two spaces

问题

我想使用Go的exec.Command()从资源管理器中打开包含两个空格的文件。

这个命令在Windows PowerShell中按预期工作。

Explorer "file://C:\Users. Sample.  Sample2"

使用Go的exec.Command()可以处理包含空格的文件名,像这样:

exec.Command(`explorer`, "file://C:\Users. Sample").CombinedOutput()

但是,如果文件名包含两个空格,像这样:

exec.Command(`explorer`, "file://C:\Users. Sample.  Sample2").CombinedOutput()

就会失败。

请告诉我如何解决这个问题。


这段代码按预期工作。谢谢。

exec.Command(`explorer`, `file://C:\Users. Sample.  Sample2`).CombinedOutput()

但实际输入是字符串(不是原始字符串字面量),如下所示。所以我认为我需要将字符串转换为原始字符串。

url := "file://C:\Users. Sample.  Sample2"

       <我需要将其转换为原始字符串字面量吗?>

result, err := exec.Command(`explorer`, url).CombinedOutput()

if err != nil {
	log.Fatal(err)
}
英文:

I want to open the file contain two spaces with explorer from Go's exec.Command().

This command works as expected from Windows PowerShell.

Explorer &quot;file://C:\Users. Sample.  Sample2&quot;

And Using Go's exec.Command() works with fileName contain space like this.

exec.Command(`explorer`, &quot;file://C:\Users. Sample&quot;).CombinedOutput()

But failed with fileName contain two spaces like this

exec.Command(`explorer`, &quot;file://C:\Users. Sample.  Sample2&quot;).CombinedOutput()

Please tell me how to solve this.


This code works as expected. Thanks.

exec.Command(`explorer`, `file://C:\Users. Sample.  Sample2`).CombinedOutput()

But actual input is string(not raw string literal) as below. So I think that I need to convert string to raw string.

url := &quot;file://C:\Users. Sample.  Sample2&quot;

       &lt;I need to convert to raw string literal?&gt; 

result, err := exec.Command(`explorer`, url).CombinedOutput()

if err != nil {
	log.Fatal(err)
}

答案1

得分: 3

在文件名中使用两个空格与运行命令失败无关。

你正在使用解释字符串字面量:

"file://C:\Users. Sample.  Sample2"

在解释字符串字面量中,反斜杠字符\是特殊的,如果你想要字符串中有一个反斜杠,你必须对其进行转义,转义序列是两个反斜杠:\\

"file://C:\\Users\. Sample\.  Sample2"

或者更好的方法是使用原始字符串字面量(这样你就不需要转义):

`file://C:\Users. Sample.  Sample2`

你文件名中奇怪的字符可能也会导致问题。如果你在Go源代码中包含它,它将被解释并编码为UTF-8字符串。你将其作为参数传递,但是你的操作系统(Windows)在检查文件名时可能使用不同的字符编码,所以它可能找不到它。如果仅仅更正字符串字面量不足以使其工作,那么请从文件名中删除那个奇怪的字符。

当你从PowerShell运行命令时,它可以工作,因为PowerShell不期望一个解释字符串,并且它使用的编码也用于存储文件名。

另外,Cmd.CombinedOutput()(就像Cmd.Run()Cmd.Start()一样)返回一个error,请确保检查它,因为它可能提供额外的信息。

英文:

Having 2 spaces in file names has nothing to do with failing to run the command.

You are using interpreted string literals:

&quot;file://C:\Users. Sample.  Sample2&quot;

In interpreted string literals the backslash character \ is special and if you want your string to have a backslash, you have to escape it, and the escape sequence is 2 backslahses: \\:

&quot;file://C:\\Users\. Sample\.  Sample2&quot;

Or even better: use raw string literals (and then you don't need to escape it):

`file://C:\Users. Sample.  Sample2`

That weird character in your file name may also cause an issue. If you include that in Go source code, it will be interpreted and encoded as UTF-8 string. You pass this as a parameter, but your OS (windows) may use a different character encoding when checking file names, so it may not find it. Remove that weird character from the file name if correcting the string literal is not sufficient to make it work.

When you run the command from PowerShell, it works because PowerShell does not expect an interpreted string and it uses the encoding that is also used to store file names.

Also Cmd.CombinedOutput() (just like Cmd.Run() and Cmd.Start()) returns an error, be sure to check that as it may provide additional info.

huangapple
  • 本文由 发表于 2016年4月21日 18:00:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/36766174.html
匿名

发表评论

匿名网友

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

确定