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