Linux下c语言实现将一个目录下的所有文件和目录复制到另一个目录下
2013-12-04 12:18
585 查看
#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;
}
转载自:http://biancheng.dnbcw.info/linux/74567.html
#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;
}
转载自:http://biancheng.dnbcw.info/linux/74567.html
相关文章推荐
- Linux下c语言实现将一个目录下的所有文件和目录复制到另一个目录下
- Linux下c语言实现将一个目录下的所有文件和目录复制到另一个目录下
- c#实现将一个文件夹中的所有文件复制到另一个文件夹,并在新文件夹中创建一个txt文件记录copy的文件综述以及文件名称
- 复制一个目录下的所有文件到另外一个目录(Java实现)
- 将一个目录下的所有文件复制到另一个目录下
- 复制一个目录下的所有文件到另外一个目录(Java实现)
- Java实现复制一个目录下的所有文件
- [PHP][Function]实现将一个文件夹下的所有文件及文件夹复制到另一个文件夹里(保持原有结构
- [PHP][Function]实现将一个文件夹下的所有文件及文件夹复制到另一个文件夹里(保持原有结构)[已验证][原创]
- 复制一个目录下文件到另一个目录下
- 将目录下面所有的 .cs 文件合并到一个 code.cs 文件中,写著作权复制代码时的必备良药
- 实验 4.2.4 实现文件复制 1. 用文件流的 I/O 函数实现一个文件拷贝程序,将一个文件拷贝到另一个文件。 2. 分别用字节读,行读,任意大小读的方式。
- C# 递归 把一个文件夹下所有文件复制到另一个文件夹下
- 编写一个程序,将 d:\java 目录下的所有.java 文件复制到 d:\jad 目录下,并将原来文件的扩展名从.java 改为.jad。
- 每天学点linux--(用shell实现将一个文件夹下面的所有文件拷贝到另一个文件夹下/shell下如何重命名一个文件)
- bat复制文件夹下所有文件到另一个目录
- 使用IO技术,创建一个目录,然后复制一个文件到该目录!实现复制的功能。(在博客园上传的第一份代码)
- 递归实现以树的形式输入某一个文件夹下的所有文件及目录,不要求图形界面
- 70.将A工程下某一个文件下除.svn外的所有文件复制备份到B工程的同一目录下
- SHELL实现取出一个目录下所有日志文件中的所有IP地址并去重