123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package main
- import (
- "errors"
- "fmt"
- "io"
- "os"
- "os/exec"
- "path/filepath"
- "runtime"
- "strings"
- )
- func GetCurrentPath() (string, error) {
- file, err := exec.LookPath(os.Args[0])
- if err != nil {
- return "", err
- }
- path, err := filepath.Abs(file)
- if err != nil {
- return "", err
- }
- //fmt.Println("path111:", path)
- if runtime.GOOS == "windows" {
- path = strings.Replace(path, "\\", "/", -1)
- }
- //fmt.Println("path222:", path)
- i := strings.LastIndex(path, "/")
- if i < 0 {
- return "", errors.New(`Can't find "/" or "\".`)
- }
- //fmt.Println("path333:", path)
- return string(path[0 : i+1]), nil
- }
- /**
- * 拷贝文件夹,同时拷贝文件夹中的文件
- * @param srcPath 需要拷贝的文件夹路径: D:/test
- * @param destPath 拷贝到的位置: D:/backup/
- */
- func CopyDir(path ...string) error {
- if len(path) < 2 {
- return errors.New("args illegal")
- }
- srcPath := ""
- for i := 0; i < len(path)-1; i++ {
- srcPath1 := path[i]
- srcPath = strings.Replace(srcPath1, "\\", "/", -1)
- srcInfo, err := os.Stat(srcPath)
- //检测目录正确性
- if err != nil {
- srcPath = ""
- continue
- }
- if !srcInfo.IsDir() {
- srcPath = ""
- } else {
- break
- }
- }
- if srcPath == "" {
- err := fmt.Errorf("src path %v error", path[:len(path)-1])
- fmt.Println(err.Error())
- return err
- }
- destPath1 := ""
- if len(path) >= 2 {
- destPath1 = path[len(path)-1]
- }
- destPath := strings.Replace(destPath1, "\\", "/", -1)
- if destInfo, err := os.Stat(destPath); err != nil {
- fmt.Println(err.Error())
- return err
- } else {
- if !destInfo.IsDir() {
- e := errors.New("destInfo不是一个正确的目录!")
- fmt.Println(e.Error())
- return e
- }
- }
- //加上拷贝时间:不用可以去掉
- //destPath = destPath + "_" + time.Now().Format("20060102150405")
- err := filepath.Walk(srcPath, func(path string, f os.FileInfo, err error) error {
- if f == nil {
- return err
- }
- if !f.IsDir() {
- path := strings.Replace(path, "\\", "/", -1)
- destNewPath := strings.Replace(path, srcPath, destPath, -1)
- fmt.Println("复制文件:" + path + " 到 " + destNewPath)
- copyFile(path, destNewPath)
- }
- return nil
- })
- if err != nil {
- fmt.Printf(err.Error())
- }
- return err
- }
- //生成目录并拷贝文件
- func copyFile(src, dest string) (w int64, err error) {
- srcFile, err := os.Open(src)
- if err != nil {
- fmt.Println(err.Error())
- return
- }
- defer srcFile.Close()
- //分割path目录
- destSplitPathDirs := strings.Split(dest, "/")
- //检测时候存在目录
- destSplitPath := ""
- for index, dir := range destSplitPathDirs {
- if index < len(destSplitPathDirs)-1 {
- destSplitPath = destSplitPath + dir + "/"
- b, _ := pathExists(destSplitPath)
- if b == false {
- fmt.Println("创建目录:" + destSplitPath)
- //创建目录
- err := os.Mkdir(destSplitPath, os.ModePerm)
- if err != nil {
- fmt.Println(err)
- }
- }
- }
- }
- dstFile, err := os.Create(dest)
- if err != nil {
- fmt.Println(err.Error())
- return
- }
- defer dstFile.Close()
- return io.Copy(dstFile, srcFile)
- }
- //检测文件夹路径时候存在
- func pathExists(path string) (bool, error) {
- _, err := os.Stat(path)
- if err == nil {
- return true, nil
- }
- if os.IsNotExist(err) {
- return false, nil
- }
- return false, err
- }
|