duandao6414 2018-04-27 13:24
浏览 758
已采纳

从Go程序中调用Windows批处理文件,以便在新窗口中打开[重复]

This question already has an answer here:

I have a batch file named cfw.bat which will accept 1 command-line parameter.

I need to invoke or open this batch file from my go program and not to run as a command within go (because command within Go is not considering window's commands).

This is my go code

    foundationUrl := foundations.GetFoundationApiUrl(foundationNumber)
    command := exec.Command("call","cfw.bat",foundationUrl)
    fmt.Print("Executing Command >>> ")
    fmt.Println(command.Args)
    bs,err :=command.CombinedOutput()

This is the error i get

Executing Command >>> [call cfw.bat https://api.sys-pcf.com]
Error exec: "call": executable file not found in %PATH%

Couldn't execute windows command.. If i directly execute using cfw.bat from go code, it won't work because the shell used by Go is only Linux / Unix supported

Please help on how to trigger the batch as a seperate window from my Go Program

</div>
  • 写回答

1条回答 默认 最新

  • doumi1884 2018-04-27 14:04
    关注

    From my comments:

    cmd := exec.Command("cmd.exe", "/C", "C:\\me\\fcf\\cfw.bat", foundationUrl)
    

    Where backslashes are doubled and you provide the full path to cfw.bat

    An alternative may be to use backticks instead:

    cmd := exec.Command(`cmd.exe`, `/C`, `C:\me\fcf\cfw.bat`, foundationUrl)
    

    If you just want to open the batch file in another cmd.exe instance then maybe this would do it:

    cmd := exec.Command(`cmd.exe`, `/C`, `Start C:\me\fcf\cfw.bat`, foundationUrl)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆