[Golang copy files to another directory]

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The main function of this program is to compare files under the A folder with the files in B directory, if found to overwrite the corresponding directory B.
Usage: Merge a directory B directory
Merge.go

 PackageMainImport("Flag"    "FMT"    "OS"    "Path/filepath"    "Strings"    "Time"        "Github.com/unknwon/com")Const(isdirectory =IotaIsregular Issymlink)typeSysfilestruct{FTypeintFNamestringFLinkstringFsizeInt64Fmtime time. Time Fperm OS. FileMode}typeFstruct{Files []*sysfile}func(self *f) visit (Pathstring, F OS. FileInfo, err Error) error {iff = =Nil{returnERR}varTpint    ifF.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}funcMain () {flag. Parse () SourceDir: = flag. Arg(0) Decdir: = flag. Arg(1) Source: = f{files: Make([]*sysfile,0),} ERR: = FilePath. Walk (SourceDir,func(Pathstring, F OS. FileInfo, err Error) error {returnSource.visit (path, F, err)})ifErr! =Nil{FMT. Printf ("filepath. Walk () returned%v\n ", err)} Dec: = f{Files: Make([]*sysfile,0),} err = filepath. Walk (Decdir,func(Pathstring, F OS. FileInfo, err Error) error {returnDec.visit (path, F, err)})ifErr! =Nil{FMT. Printf ("filepath. Walk () returned%v\n ", err)} for_, V: =RangeSource.files {ifCom. Isfile (v.fname) = =true{tmp1: = strings. Split (V.fname,"\\") SourceName: = tmp1[Len(TMP1)-1] for_, R: =RangeDec.files {ifCom. Isfile (r.fname) = =true{tmp2: = strings. Split (R.fname,"\\") Decname: = tmp2[Len(TMP2)-1]ifSourceName = = Decname {fmt. Printf ("The same file:%s\n", SourceName) com. Copy (V.fname, R.fname)}}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.