简体   繁体   English

失败的exec.Command和FileName包含两个空格

[英]Failed exec.Command with fileName contain two spaces

I want to open the file contain two spaces with explorer from Go's exec.Command() . 我想用Go的exec.Command()从资源管理器打开包含两个空格的文件。

This command works as expected from Windows PowerShell. 该命令可以在Windows PowerShell中正常运行。

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

And Using Go's exec.Command() works with fileName contain space like this. 并且使用Go的exec.Command()与fileName一起包含这样的空间。

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

But failed with fileName contain two spaces like this 但是失败,fileName包含两个这样的空格

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

Please tell me how to solve this. 请告诉我如何解决这个问题。


This code works as expected. 此代码按预期方式工作。 Thanks. 谢谢。

exec.Command(`explorer`, `file://C:\Users\1. Sample\2.  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 := "file://C:\Users\1. Sample\2.  Sample2"

       <I need to convert to raw string literal?> 

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

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

Having 2 spaces in file names has nothing to do with failing to run the command. 文件名中有2个空格与无法运行命令无关。

You are using interpreted string literals : 您正在使用解释的字符串文字

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

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: \\\\ : 在解释的字符串文字中,反斜杠字符\\是特殊字符,如果希望string具有反斜杠,则必须对其进行转义,转义序列为2个反斜杠: \\\\

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

Or even better: use raw string literals (and then you don't need to escape it): 甚至更好:使用原始字符串文字 (然后您无需转义):

`file://C:\Users\1. Sample\2.  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. 如果将其包含在Go源代码中,它将被解释并编码为UTF-8字符串。 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. 您将其作为参数传递,但是您的操作系统(Windows)在检查文件名时可能使用不同的字符编码,因此可能找不到。 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. 当您从PowerShell运行命令时,它可以工作,因为PowerShell不需要解释的字符串,并且它使用的编码也用于存储文件名。

Also Cmd.CombinedOutput() (just like Cmd.Run() and Cmd.Start() ) returns an error , be sure to check that as it may provide additional info. 同样, Cmd.CombinedOutput() (就像Cmd.Run()Cmd.Start() )返回error ,请确保检查它是否可以提供其他信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM