You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
797 B

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package utils
  2. import (
  3. "os"
  4. "path/filepath"
  5. )
  6. //@author: [songzhibin97](https://github.com/songzhibin97)
  7. //@function: FileMove
  8. //@description: 文件移动供外部调用
  9. //@param: src string, dst string(src: 源位置,绝对路径or相对路径, dst: 目标位置,绝对路径or相对路径,必须为文件夹)
  10. //@return: err error
  11. func FileMove(src string, dst string) (err error) {
  12. if dst == "" {
  13. return nil
  14. }
  15. src, err = filepath.Abs(src)
  16. if err != nil {
  17. return err
  18. }
  19. dst, err = filepath.Abs(dst)
  20. if err != nil {
  21. return err
  22. }
  23. var revoke = false
  24. dir := filepath.Dir(dst)
  25. Redirect:
  26. _, err = os.Stat(dir)
  27. if err != nil {
  28. err = os.MkdirAll(dir, 0755)
  29. if err != nil {
  30. return err
  31. }
  32. if !revoke {
  33. revoke = true
  34. goto Redirect
  35. }
  36. }
  37. return os.Rename(src, dst)
  38. }