1. 程式人生 > 程式設計 >Golang 實現複製資料夾同時複製檔案

Golang 實現複製資料夾同時複製檔案

Golang 複製資料夾,包括資料夾中的檔案

/**
 * 拷貝資料夾,同時拷貝資料夾中的檔案
 * @param srcPath 需要拷貝的資料夾路徑: D:/test
 * @param destPath 拷貝到的位置: D:/backup/
 */
func CopyDir(srcPath string,destPath string) error {
 //檢測目錄正確性
 if srcInfo,err := os.Stat(srcPath); err != nil {
 fmt.Println(err.Error())
 return err
 } else {
 if !srcInfo.IsDir() {
 e := errors.New("srcPath不是一個正確的目錄!")
 fmt.Println(e.Error())
 return e
 }
 }
 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
}

補充:golang把檔案複製到另一個目錄

本程式 主要功能是把A資料夾下的檔案與B目錄下檔案對比,如果找到就覆蓋到B相應的目錄下。

用法: merge A目錄 B目錄

merge.go

package main
import (
 "flag"
 "fmt"
 "os"
 "path/filepath"
 "strings"
 "time"
  "github.com/Unknwon/com"
)
const (
 IsDirectory = iota
 IsRegular
 IsSymlink
)
type sysFile struct {
 fType int
 fName string
 fLink string
 fSize int64
 fMtime time.Time
 fPerm os.FileMode
}
type F struct {
 files []*sysFile
}
func (self *F) visit(path string,err error) error {
 if f == nil {
  return err
 }
 var tp int
 if f.IsDir() {
  tp = IsDirectory
 } else if (f.Mode() & os.ModeSymlink) > 0 {
  tp = IsSymlink
 } else {
  tp = IsRegular
 }
 inoFile := &sysFile{
  fName: path,fType: tp,fPerm: f.Mode(),fMtime: f.ModTime(),fSize: f.Size(),}
 self.files = append(self.files,inoFile)
 return nil
}
func main() {
 flag.Parse()
 sourcedir := flag.Arg(0)
 decdir := flag.Arg(1)
 source := F{
  files: make([]*sysFile,0),}
 err := filepath.Walk(sourcedir,err error) error {
  return source.visit(path,f,err)
 })
 if err != nil {
  fmt.Printf("filepath.Walk() returned %v\n",err)
 }
 dec := F{
  files: make([]*sysFile,}
 err = filepath.Walk(decdir,err error) error {
  return dec.visit(path,err)
 }
 for _,v := range source.files {
  if com.IsFile(v.fName) == true {
   tmp1 := strings.Split(v.fName,"\\")
   sourcename := tmp1[len(tmp1)-1]
   for _,r := range dec.files {
    if com.IsFile(r.fName) == true {
     tmp2 := strings.Split(r.fName,"\\")
     decname := tmp2[len(tmp2)-1]
     if sourcename == decname {
      fmt.Printf("the same file: %s\n",sourcename)
      com.Copy(v.fName,r.fName)
     }
    }
   }
  }
 }
}

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援我們。如有錯誤或未考慮完全的地方,望不吝賜教。