Windows command line additional quotation mark in parameter in Golang

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

Windows command line additional quotation mark in parameter in Golang

问题

我想在Windows(Windows 8.1和Go 1.3.1)中编写一个批处理助手来使用exiftool.exe。

我之所以要在Go中运行命令行,是因为我尝试访问一些从其他网站检索到的EXIF信息。我只想将其写回图片的EXIF中。

以下是我的代码片段:

str_abs, _ := filepath.Abs(target_path)
str_title := fmt.Sprintf("-title=\"%s\"", ext_str)
stdout, err := exec.Command("cmd", "/c", "exiftool.exe", str_title, "-E", str_abs).Output()

然而,我发现参数中总是会有一个额外的引号,导致结果与以下内容相同:

exiftool.exe -title=""TITLE"" -E TARGET_FILE

有任何想法是怎么发生的?或者对于如何处理这样的参数有任何建议。

注意:

  • 我将参数和字符串分开再组合,是因为命令行需要使用Unicode字符串,比如中文字符串。
  • exiftool.exe只能在Windows的命令行中使用带有中文字符的"&#%d;"代码,所以我对此进行了更多处理。
  • 但是,如果我使用非Unicode字符串(例如,标题显示为'1234'而不是1234),也会出现错误。
stdout, err := exec.Command("cmd", "/c", "exiftool.exe", "-title", "1234", str_abs).Output()
  • 错误代码:运行命令错误:退出状态1。

-----更新于2014/09/10,针对@VonC-------------------------------------------------

嗨VonC,

我尝试按照您提供的代码做同样的事情,但对我来说不起作用。
我对命令行代码页感到好奇,我尝试了477(美国)和950(Big5),都对我无效。

有两件事我想讨论一下。

  • 中文必须更改为"&#%d;",否则会显示错误。
  • 即使我尝试了,但我的图片标题仍然显示额外的引号。
  • 我的代码示例如下:
output, err := exec.Command(`d:\exiftool.exe`,
    `-title="test 2世界"`,
    //如果它没有从世界 -> 世界 转换,它将无法正确显示。
    "-E", "test.jpg").CombinedOutput()
if err != nil {
    fmt.Println(err.Error())
}
fmt.Println(string(output))

希望对你有帮助!

英文:

I would like to write a batch helper in Windows (Windows 8.1 with Go 1.3.1) to using exiftool.exe.

The reason I to run command line in Go is I tried to access some EXIF information which I retrieval from other web side. I just want to write back to picture EXIF.

Here is my code segment.

		str_abs, _ := filepath.Abs(target_path)
		str_title := fmt.Sprintf("-title=\"%s\"", ext_str)
		stdout, err := exec.Command("cmd", "/c", "exiftool.exe", str_title, "-E", str_abs).Output()

However I found there always a addtional quote will in parameter so the result will identical with:

exiftool.exe -title=""TITLE"" -E TARGET_FILE

Any idea how it happen? or any suggestion for how to handle parameter like this.

Note:

  • The reason I separate parameter and string combine it again, because command line need use unicode string such as Chinese String.

  • exiftool.exe only works on command line with "&#%d;" code with Chinese Character in Windows so I do more handle for this.

  • How ever it also happen error if I use non-unicode such as (title shows '1234' not 1234)

     	stdout, err := exec.Command("cmd", "/c", "exiftool.exe", "-title", "1234", str_abs).Output()
    
  • error code:Run command eror: exit status 1.

-----Update on 2014/09/10 for @VonC-------------------------------------------------

Hi VonC,

I tried to do the same thing as your provided code, but it not works for me.
I am curious about command line code page, I tried it in 477(US) 950 (Big5) both not work for me.

There is two thing I would like to discuss.

  • The Chinese word must change to &#%d; otherwise it will wrong display.

  • Even I tried it, but it still display extra " in my picture title.

  • My code sample as follow:

      output, err := exec.Command(`d:\exiftool.exe`,
      `-title="test 2世界"`,
      //If it not trasnfer from 世界 -> 世界  it could not display correctly.
      "-E", "test.jpg").CombinedOutput()
      if err != nil {
          fmt.Println(err.Error())
      }
      fmt.Println(string(output))
    

答案1

得分: 1

我刚刚进行了测试:

output, err := exec.Command(`c:\prgs\exiftool-9.70\exiftool.exe`,
    `-title="test 2 世界"`,
    "-E", "test.jpg").CombinedOutput()
if err != nil {
    fmt.Println(err.Error())
}
fmt.Println(string(output))

看起来一切正常。
请注意,exec.CombinedOutput()在出现问题时可以显示比"exit status 1"更多的信息。

第二次调用将显示所有的EXIF元数据:

exec.Command(`c:\prgs\exiftool-9.70\exiftool.exe`, "test.jpg").CombinedOutput()

标题应该是预期的test 2 世界

英文:

I just tested:

output, err := exec.Command(`c:\prgs\exiftool-9.70\exiftool.exe`,
	`-title="test 2 世界"`,
	"-E", "test.jpg").CombinedOutput()
if err != nil {
	fmt.Println(err.Error())
}
fmt.Println(string(output))

And it seems to work just fine.
Note that exec.CombinedOutput() allows to see a bit more than just "exit status 1" in case of a problem.

A second call will dispay all EXIF matadata:

exec.Command(`c:\prgs\exiftool-9.70\exiftool.exe`, "test.jpg").CombinedOutput()

The title is the expected test 2 世界.

答案2

得分: 0

fmt.Printf("%q", "somestring") => "somestring"

英文:

fmt.Printf("%q", "somestring") => "somestring"

huangapple
  • 本文由 发表于 2014年9月9日 11:00:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/25736072.html
匿名

发表评论

匿名网友

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

确定