gpt4 book ai didi

go - Windows命令行在Golang中的参数中附加引号

转载 作者:IT王子 更新时间:2023-10-29 02:05:53 25 4
gpt4 key购买 nike

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

我在 Go 中运行命令行的原因是我试图访问我从其他 Web 端检索的一些 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 仅适用于带有“&#%d;”的命令行Windows 中带有中文字符的代码,所以我对此做了更多处理。
  • 如果我使用非 unicode,例如(标题显示“1234”而不是 1234),它怎么也会发生错误

        stdout, err := exec.Command("cmd", "/c", "exiftool.exe", "-title", "1234", str_abs).Output()
  • 错误代码:运行命令错误:退出状态1。

-----@VonC 于 2014/09/10 更新---------------------------- ------------------

你好 VonC,

我尝试按照您提供的代码做同样的事情,但它对我不起作用。我对命令行代码页很好奇,我在 477(US) 950 (Big5) 中尝试过,但两者都不适合我。

我想讨论两件事。

  • 中文单词必须改成&#%d;否则会显示错误。
  • 即使我试过了,但它仍然在我的图片标题中显示额外的 "。
  • 我的代码示例如下:

    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))

最佳答案

我刚刚测试过:

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()

标题是预期的测试2世界

关于go - Windows命令行在Golang中的参数中附加引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25736072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com