CopyDir recursive copy of directory
(source string, dest string)
| 35 | |
| 36 | // CopyDir recursive copy of directory |
| 37 | func CopyDir(source string, dest string) (err error) { |
| 38 | // get properties of source dir |
| 39 | sourceinfo, err := os.Stat(source) |
| 40 | if err != nil { |
| 41 | return err |
| 42 | } |
| 43 | |
| 44 | // create dest dir |
| 45 | |
| 46 | err = os.MkdirAll(dest, sourceinfo.Mode()) |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | |
| 51 | objects, err := os.ReadDir(source) |
| 52 | |
| 53 | for _, obj := range objects { |
| 54 | sourcefilepointer := source + "/" + obj.Name() |
| 55 | |
| 56 | destinationfilepointer := dest + "/" + obj.Name() |
| 57 | |
| 58 | if obj.IsDir() { |
| 59 | // create sub-directories - recursively |
| 60 | err = CopyDir(sourcefilepointer, destinationfilepointer) |
| 61 | if err != nil { |
| 62 | fmt.Println(err) |
| 63 | } |
| 64 | } else { |
| 65 | // perform copy |
| 66 | err = CopyFile(sourcefilepointer, destinationfilepointer) |
| 67 | if err != nil { |
| 68 | fmt.Println(err) |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | return err |
| 73 | } |