[golang把檔案複製到另一個目錄]

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

//本程式 主要功能是把A檔案夾下的檔案與B目錄下檔案對比,如果找到就覆蓋到B相應的目錄下。
// 用法: merge A目錄 B目錄
// merge.go

package mainimport (    "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, f os.FileInfo, 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, func(path string, f os.FileInfo, 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, 0),    }    err = filepath.Walk(decdir, func(path string, f os.FileInfo, err error) error {        return dec.visit(path, f, err)    })    if err != nil {        fmt.Printf("filepath.Walk() returned %v\n", 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)                    }                }            }        }    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.