directory.go 550 B

1234567891011121314151617181920212223242526
  1. package directory
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. )
  7. // MoveToSubdir moves all contents of a directory to a subdirectory underneath the original path
  8. func MoveToSubdir(oldpath, subdir string) error {
  9. infos, err := ioutil.ReadDir(oldpath)
  10. if err != nil {
  11. return err
  12. }
  13. for _, info := range infos {
  14. if info.Name() != subdir {
  15. oldName := filepath.Join(oldpath, info.Name())
  16. newName := filepath.Join(oldpath, subdir, info.Name())
  17. if err := os.Rename(oldName, newName); err != nil {
  18. return err
  19. }
  20. }
  21. }
  22. return nil
  23. }