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

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

How to delete a folder with space in the name(Golang)

问题

以下代码适用于Windows系统:

//假设我在C:\temp目录下有一个名为dname的文件夹
mydir := "C:\\temp\\dname"
cmd, e := exec.Command("cmd", "/C", "rmdir /S /Q", mydir).Output()

但是,如果文件夹名称中有空格,例如:

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

上述代码将失败。Golang的os.RemoveAll函数可以处理带有空格的文件夹名称,但在以下情况下会失败:

//创建一个名为myprj的文件夹
C:\> mkdir myprj
C:\> cd myprj
C:\myprj> git init
//添加一些文件
C:\myprj> git add .
C:\myprj> git commit -m "Add my files"
//
//下面的代码将无法删除文件夹
err := os.RemoveAll("C:/myprj")

有没有办法在Windows中使用Go完全删除文件夹?

更新1

无论是\\还是/都会出现相同的错误:

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

但是,我可以使用以下命令删除文件夹:

C:\> rmdir /S /Q c:\temp\myprj
//或者通过Windows资源管理器删除文件夹,没有任何问题
英文:

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\\name 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\objectsb8ef36e172ae05842a9326fc73f1c8baa3254: 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

答案1

得分: 0

我在Windows上做了同样的事情,也遇到了同样的错误。但是我注意到我的myprj文件夹是只读的(在属性中),所以我将其关闭后,代码就可以正常工作了。

实际上,我在C盘上创建的每个文件夹默认都是“只读”的。

而对于一个空文件夹,属性中显示为“只读(仅适用于文件夹中的文件)”。

英文:

I did the same thing on my windows and got the same error too. But I noticed the myprj folder is readonly (in properties) so I turned it off then the code works.

Actually every folder I created on c:\ is "readonly" by default.

And this is not apply to an empty folder the properties says "Read-Only (Only applies to files in folder)".

答案2

得分: 0

我发现这种方法很有效:

  1. 创建一个批处理文件 myrmdir.bat
    @echo off
    RMDIR /S /Q %1
  1. 将此文件放在与代码相同的文件夹中,并使用以下方式调用它:
    //是的,我可以使用 / 而不是 \\,它也可以工作
    myfolder := "c:/temp/folder with space"
    exec.Command("cmd", "/C", "myrmdir.bat", myfolder).Run()

希望这对你有帮助。如果你有更好的解决方案(尤其是纯Go语言解决方案),请告诉我。

英文:

I found this method works well:

  1. Create a batch file myrmdir.bat:
    @echo off
    RMDIR /S /Q %1
  1. 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.

huangapple
  • 本文由 发表于 2014年7月15日 06:05:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/24746793.html
匿名

发表评论

匿名网友

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

确定