Golang 实现复制文件夹同时复制文件
作者:印象丶亮仔 时间:2024-02-09 10:57:26
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, 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)
}
}
}
}
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/qq_23179075/article/details/83347698
标签:Golang,复制,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PHP运行环境配置与开发环境的配置(图文教程)
2024-05-11 09:24:46
![](https://img.aspxhome.com/file/2023/5/126215_0s.png)
详解Python调试神器之PySnooper
2021-05-20 06:51:12
![](https://img.aspxhome.com/file/2023/3/99023_0s.png)
SQL Server服务器内存升级后的烦恼
2008-12-22 10:55:00
python搭建微信公众平台
2022-05-24 00:14:09
C#使用checkedListBox1控件链接数据库的方法示例
2024-01-24 19:15:09
![](https://img.aspxhome.com/file/2023/8/94578_0s.jpg)
Python读取分割压缩TXT文本文件实例
2023-11-04 11:17:27
pycharm中import导入包呈现灰色的问题及解决
2023-10-03 23:12:10
![](https://img.aspxhome.com/file/2023/4/68304_0s.png)
使用APScheduler3.0.1 实现定时任务的方法
2023-12-12 04:16:05
jenkins自动构建发布vue项目的方法步骤
2024-04-30 10:47:14
![](https://img.aspxhome.com/file/2023/5/130045_0s.png)
利用python实现后端写网页(flask框架)
2022-06-22 04:20:50
![](https://img.aspxhome.com/file/2023/2/89272_0s.png)
Python中垃圾回收和del语句详解
2023-12-20 01:02:55
基于python实现坦克大战游戏
2023-01-29 01:48:30
![](https://img.aspxhome.com/file/2023/9/133539_0s.jpg)
python设置Pyplot的动态rc参数、绘图的填充
2023-12-15 22:52:29
![](https://img.aspxhome.com/file/2023/9/92169_0s.png)
Django model重写save方法及update踩坑详解
2021-01-21 08:45:52
详解Python迭代和迭代器
2023-04-30 23:31:13
Python语法学习之进程间的通信方式
2023-04-03 11:30:28
![](https://img.aspxhome.com/file/2023/0/101330_0s.jpg)
keras K.function获取某层的输出操作
2023-03-11 15:10:21
Python实现绘制多种激活函数曲线详解
2022-06-30 01:02:06
![](https://img.aspxhome.com/file/2023/8/78488_0s.png)
mysql 5.7.14 下载安装配置方法图文教程
2024-01-21 00:12:58
![](https://img.aspxhome.com/file/2023/0/106590_0s.jpg)
Python selenium如何设置等待时间
2023-08-31 18:53:39