dovgqc2648 2014-07-14 22:05
浏览 127
已采纳

如何删除名称中带有空格的文件夹(Golang)

The following codes works in windows:

//suppose I have a fname folder in c:\temp
mydir := "C:\\temp\\dname"
cmd, e := exec.Command("cmd", "/C", " rmdir /S /Q", mydir).Output()

But it will failed if there are spaces in the folder name, like:

mydir := "C:\\temp\
ame with space"

The Golang os.RemoveAll can handle the folder name with spaces, but it will fail in the following situation:

C:\> mkdir myprj
C:\> cd myprj
C:\myprj> git init
//add some file
C:\myprj> git add .
C:\myprj> git commit -m "Add my files"
//
//This won't work
err := os.RemoveAll("C:/myprj")

Any ideas on how to remove a folder completely in windows using Go?

Update 1

Either \\ or /is the same error:

func main() {
    if e := os.RemoveAll("c:\\temp\\myprj"); e != nil {
        fmt.Println(e)
    }
}
//OUTPUT
remove c:\temp\myprj\.git\objects\2b\018ef36e172ae05842a9326fc73f1c8baa3254: Access is denied.

But I can delete the folder with this command:

C:\> rmdir /S /Q c:\temp\myprj
// or from windows file explore without any problem
  • 写回答

2条回答 默认 最新

  • dpict99695329 2014-07-15 17:22
    关注

    I found this method works well:

    1. Create a batch file myrmdir.bat: @echo off RMDIR /S /Q %1

    2. Place this file in the same folder as the code and call it with: //Yes, I can use / instead of \\ and it works myfolder := "c:/temp/folder with space" exec.Command("cmd", "/C", "myrmdir.bat", myfolder).Run()

    Hope this helps you. If you have a better solution(esp. pure Golang solution) please let me know.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?