文件夹之间的拷贝
来源:互联网 发布:macbook 安装windows 编辑:程序博客网 时间:2024/05/22 04:58
VC实现:
方法一:
#include <shellapi.h>BOOL CopyDir(LPCTSTR lpszSrcDir, LPCTSTR lpszDstDir){ SHFILEOPSTRUCT sfo; ZeroMemory(&sfo, sizeof(sfo)); sfo.wFunc = FO_COPY; sfo.pFrom = lpszSrcDir; sfo.pTo = lpszDstDir; sfo.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR; int ret = SHFileOperation(&sfo); if ( ret == 0 ) return TRUE; else return FALSE;}
方法二:
void copydir(char* src,char* dst) { WIN32_FIND_DATA FindFileData; HANDLE hFind; char tmpsrc[256]; strcpy(tmpsrc,src); strcat(tmpsrc,"//*.*"); hFind = FindFirstFile(tmpsrc, &FindFileData); if(hFind == INVALID_HANDLE_VALUE) return; CreateDirectory(dst,0); do { char newdst[256]; strcpy(newdst,dst); if(newdst[strlen(newdst)]!='//') strcat(newdst,"//"); strcat(newdst,FindFileData.cFileName); char newsrc[256]; strcpy(newsrc,src); if(newsrc[strlen(newsrc)]!='//') strcat(newsrc,"//"); strcat(newsrc,FindFileData.cFileName); if(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) { if(strcmp(FindFileData.cFileName,".")!=0&&strcmp(FindFileData.cFileName,"..")!=0) { copydir(newsrc,newdst); } }else { CopyFile(newsrc,newdst,false); } }while(FindNextFile(hFind,&FindFileData)); FindClose(hFind); }
Linux下实现:
#include<stdio.h>#include<fcntl.h>#include<unistd.h>#include<sys/stat.h>#include<sys/types.h>#include<dirent.h>#include<string.h>char paths[1000],patht[1000],temp_paths[1000],temp_patht[1000];void Copy(char *spathname,char *tpathname){ int sfd,tfd; struct stat s,t; char c; sfd=open(spathname,O_RDONLY); tfd=open(tpathname,O_RDWR|O_CREAT); while(read(sfd,&c,1)>0) write(tfd,&c,1); fstat(sfd,&s); chown(tpathname,s.st_uid,s.st_gid); chmod(tpathname,s.st_mode); close(sfd); close(tfd);}void d_copy(char *spathname,char *tpathname){ struct stat s,t,temp_s,temp_t; struct dirent *s_p; DIR *dirs,*dirt; stat(spathname,&s); mkdir(tpathname,s.st_mode); chown(tpathname,s.st_uid,s.st_gid); dirt=opendir(tpathname); dirs=opendir(spathname); strcpy(temp_paths,spathname); strcpy(temp_patht,tpathname); while((s_p=readdir(dirs))!=NULL) { if(strcmp(s_p->d_name,".")!=0&&strcmp(s_p->d_name,"..")!=0) { strcat(temp_paths,"/"); strcat(temp_paths,s_p->d_name); strcat(temp_patht,"/"); strcat(temp_patht,s_p->d_name); lstat(temp_paths,&s); temp_s.st_mode=s.st_mode; if(S_ISLNK(temp_s.st_mode)) { printf("%s is a symbol link file/n",temp_paths); } else if(S_ISREG(temp_s.st_mode)) { printf("Copy file %s ....../n",temp_paths); Copy(temp_paths,temp_patht); strcpy(temp_paths,spathname); strcpy(temp_patht,tpathname); } else if(S_ISDIR(temp_s.st_mode)) { printf("Copy directory %s ....../n",temp_paths); d_copy(temp_paths,temp_patht); strcpy(temp_paths,spathname); strcpy(temp_patht,tpathname); } } }}int main(){ struct dirent *sp,*tp; char spath[1000],tpath[1000],temp_spath[1000],temp_tpath[1000]; struct stat sbuf,tbuf,temp_sbuf,temp_tbuf; char sdirect[1000],tdirect[1000],judge; DIR *dir_s,*dir_t; printf("Please input the sourse direct path and name :"); scanf("%s",sdirect); dir_s=opendir(sdirect); if(dir_s==NULL) { printf("This directory don't exist !/n"); return 0; } if(stat(sdirect,&sbuf)!=0) { printf("Get status error !/n"); return 0; } printf("Please input the target direct path and name :"); scanf("%s",tdirect); dir_t=opendir(tdirect); if(dir_t==NULL) { mkdir(tdirect,sbuf.st_mode); chown(tdirect,sbuf.st_uid,sbuf.st_gid); dir_t=opendir(tdirect); } else { chmod(tdirect,sbuf.st_mode); chown(tdirect,sbuf.st_uid,sbuf.st_gid); } strcpy(spath,sdirect); strcpy(tpath,tdirect); strcpy(temp_spath,sdirect); strcpy(temp_tpath,tdirect);///////////////////////////////////////////////////////////////////////////////// printf("Begin copy ......../n"); while((sp=readdir(dir_s))!=NULL) { if(strcmp(sp->d_name,".")!=0&&strcmp(sp->d_name,"..")!=0) { strcat(temp_spath,"/"); strcat(temp_spath,sp->d_name); strcat(temp_tpath,"/"); strcat(temp_tpath,sp->d_name); lstat(temp_spath,&sbuf); temp_sbuf.st_mode=sbuf.st_mode; if(S_ISLNK(temp_sbuf.st_mode)) { printf("%s is a symbolic link file/n",temp_spath); } else if((S_IFMT&temp_sbuf.st_mode)==S_IFREG) { printf("Copy file %s ....../n",temp_spath); Copy(temp_spath,temp_tpath); strcpy(temp_tpath,tpath); strcpy(temp_spath,spath); } else if((S_IFMT&temp_sbuf.st_mode)==S_IFDIR) { printf("Copy directory %s ....../n",temp_spath); d_copy(temp_spath,temp_tpath); strcpy(temp_tpath,tpath); strcpy(temp_spath,spath); } } } printf("Copy end !/n"); closedir(dir_t); closedir(dir_s); return 1;}
- 文件夹之间的拷贝
- 文件夹之间的拷贝
- linux的 拷贝文件夹
- 文件夹的拷贝
- 文件夹的拷贝
- 文件夹的拷贝
- linuc 文件夹的拷贝
- 文件之间的拷贝
- 带进度条的文件夹拷贝
- 递归实现文件夹的拷贝
- java实现文件夹的拷贝
- Virtualbox主机和虚拟机之间文件夹共享及双向拷贝
- linux两台服务器之间文件/文件夹拷贝
- 完成内存之间的拷贝
- bean之间的相互拷贝
- 顺序容器之间的拷贝
- 拷贝文件夹
- 拷贝文件夹
- 黑马程序员之 ASP.NET学习笔记:GridView控件的常用属性及方法
- hdu 1215(因数)
- 黑马程序员之 ASP.NET学习笔记:ADO.NET基础知识总结
- 判断ip地址是否合法
- 把分页显示到固定的模板上
- 文件夹之间的拷贝
- vs2008建立DLL的过程和调用过程
- 求1000以内的完数
- 计算编辑距离的算法
- PADS画板外形导入与打印
- 计算结果(利用循环)
- 天易14----JQuery实现简单的联级菜单制作
- 二叉树的非递归遍历
- (WebKit) ViewPort + Backing Store + Page Content