`
yidongkaifa
  • 浏览: 4063772 次
文章分类
社区版块
存档分类
最新评论

文件夹之间的拷贝

 
阅读更多

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;
}


分享到:
评论

相关推荐

    Virtualbox主机和虚拟机之间文件夹共享及双向拷贝(Windows&lt;-&gt;Windows, Windows&lt;-&gt;Linux)

    本篇文章主要是介绍了Virtualbox主机和虚拟机之间文件夹共享及双向拷贝,有需要的可以了解一下。

    AIX 二台机器之间如何复制文件夹

    解决二台AIX机器之前互相复制文件夹的问题

    FastCopy 数据高速完整拷贝(复制)自动效验比对工具

    软件对于拷贝和删除小文件或文件夹来讲,可能就有点大材小用了。对于大型的文件和文件夹就不同了,它能非常迅速的完成要拷贝的内容,通过简单的设置还能过滤不要的内容。体积小、方便携带、操作迅速。

    Virtualbox主机和虚拟机之间文件夹共享及双向拷贝(Windows-Windows, Windows-Linux)

    最近学习Virtualbox的一些知识,记录下,Virtualbox下如何实现主机和虚拟机之间文件夹共享及双向拷贝 关于双向拷贝 1.设置虚拟机为“双向”共享粘贴 有的人反应只要设置双向粘贴就可以,但是我的不行,我还需要再...

    java ftp服务器copy命令实现

    java程序调用API 实现在ftp内文件夹和文件的拷贝,ftp服务器之间的文件和文件夹的拷贝 欢迎来群:41229007 QQ:119346711讨论

    vb SHFileOperation 实现复制文件夹 示例源码

    如果使用了FOF_MULTIDESTFILES标志,可以包括多个文件名,文件名之间以Chr(0)分割。 - fFlags - 标志: - FOF_ALLOWUNDO - 允许恢复 - FOF_FILESONLY - 如果使用了*.*,只操作文件。 - FOF_MULTIDESTFILES -...

    文件系统账户权限迁(NETWORK SERVICE)

    在windows文件服务器之间拷贝文件时,由NETWORK SERVICE账户权限控制的文件或文件夹在复制粘帖时会出现账户权限丢失的问题。次工具则是对该账户权限查找并设置相应的文件或文件夹。 W3WP.exe所使用的账户为NETWORK ...

    浅析CentOS8虚拟机访问Windows10主机文件夹方法

    安装VMware Tools后,①文本在虚拟机主机之间可以相互复制粘贴, ②虚拟机文件可复制到主机,但 → 主机文件无法复制到虚拟机。可通过以下方法,从终端直接访问主机文件夹。 1.虚拟机&gt;设置&gt;选项&gt;共享文件夹 点选 ...

    Allway Sync 9.1.17

    它可以在几个文件夹之间进行文件同步,自动将更新的文件覆盖几个同步文件夹中的旧文件。软件带有一个小型数据库,监视每次更新后的文件状态。如果一次同步之后,你删除了同步文件夹中某些文件,它在同步的时候将...

    AllwaySync(同步软件)

    它可以在几个文件夹之间进行文件同步,自动将更新的文件覆盖几个同步文件夹中的旧文件。软件带有一个小型数据库,监视每次更新后的文件状态。如果一次同步之后,你删除了同步文件夹中某些文件,它在同步的时候将...

    AllwaySync16.0.1最新注册版

    它可以在几个文件夹之间进行文件同步,自动将更新的文件覆盖几个同步文件夹中的旧文件。软件带有一个小型数据库,监视每次更新后的文件状态。如果一次同步之后,你删除了同步文件夹中某些文件,它在同步的时候将...

    Allway Sync 9.2.11

    它可以在几个文件夹之间进行文件同步。同步方式有源文件夹同步和各向同步两种方式: 一、源文件夹同步方式将以一个文件夹为基准,删除或覆盖其余文件夹与源文件相比较不相同的文件。 二、各向同步方式则自动将更新...

    win7和win10访问共享其他资源速度慢的解决办法.txt

    win7和win10访问共享其他资源速度慢的解决办法,解决共享文件在计算机之间拷贝慢的问题。

    免费开源的文件夹比较和同步软件 FreeFileSync 11.10 中文免费版.zip

    首先是文件同步功能,可以在指定的两个文件夹之间进行单向或双向的同步,点击程序窗口右上方的绿色齿轮图标可以设置相关参数;期间也可以自定义需要筛选的文件,有“包括”和“例外”两种过滤方式,定义需要过滤的...

    Windows 复制拷贝增强工具 Fastcopy 3.89 + x64.zip

    /skip_empty_dir 启用过滤,不拷贝空文件夹(在/skip_empty_dir=FALSE抑制) /job=任务名称 执行指定的任务 /force_start 在其他的FastCopy拷贝,并且正执行的时候,执行立即也(在/force_start=FALSE抑制) /disk_...

    linux Shell获取某目录下所有文件夹的名称

    查看目录下面的所有文件: #!/bin/bash cd /目标目录 for file in $(ls *) do echo $file done ... 您可能感兴趣的文章:Virtualbox主机和虚拟机之间文件夹共享及双向拷贝(Windows&lt;-&gt;Windows, Win

    文件同步工具 FreeFileSync V12.2

    将一台电脑文件拷贝到另一台电脑,文件...FreeFileSync 不是每次都复制每个文件,而是确定 源文件夹和目标文件夹之间的差异,并且仅传输所需的最小数据量。 FreeFileSync 是开源软件,可用于 Windows、macOS 和 Linux。

    使用scp在linux主机之间复制文件目录

    使用scp在linux主机之间复制文件目录

    Python实现Windows和Linux之间互相传输文件(文件夹)的方法

    下面小编就为大家带来一篇Python实现Windows和Linux之间互相传输文件(文件夹)的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    AllwaySync9.4.11专业版注册,适用所有版本(原创10分)

    它可以在几个文件夹之间进行文件同步。同步方式有源文件夹同步和各向同步两种方式: 一、源文件夹同步方式将以一个文件夹为基准,删除或覆盖其余文件夹与源文件相比较不相同的文件。 二、各向同步方式则自动将更新...

Global site tag (gtag.js) - Google Analytics