Delete a directory and all of the subdirectories using os/exec in Windows

1,400 views
Skip to first unread message

Adrian Stanciu

unread,
Jan 13, 2014, 12:36:44 PM1/13/14
to golan...@googlegroups.com

This is what I tried:

package main

import (
    "fmt"
    "os/exec"
)

func main() {
    fmt.Println("Removing build directory")
    if err := exec.Command("cmd", "/S /Q", "RD", "c:\\build").Run(); err != nil {
        fmt.Printf("Error removing build directory: %s\n", err)
    }

    if err := exec.Command("cmd", "/C", "mkdir", "c:\\build").Run(); err != nil {
        fmt.Printf("Error making new build directory: %s\n", err)
    }
}

And my output is:

Removing build directory
Error making new build directory: exit status 1

So I don't get any error when removing, but it doesn't delete anything. If I do this from command prompt: rd /s /q c:\\build, it works ok.

Why is that ?

Adrian Stanciu

unread,
Jan 13, 2014, 1:27:12 PM1/13/14
to golan...@googlegroups.com
Found my answer: c := exec.Command("cmd", "/C", "rd /S /Q", "C:\\build")

Francesc Campoy Flores

unread,
Jan 13, 2014, 1:29:31 PM1/13/14
to Adrian Stanciu, golan...@googlegroups.com
You could also use RemoveAll which will work cross-platform:



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
--
Francesc Campoy
Reply all
Reply to author
Forward
0 new messages