github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/directory/directory.go (about)

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