您的位置:首页 > 编程语言 > C语言/C++

如何读取所有文件夹下的所有文件

2013-12-02 00:02 423 查看
#include <stdio.h>
#include <stdlib.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <string.h>

//判断是否为目录

int IS_DIR(const char* path)
{
struct stat st;
lstat(path, &st);
return S_ISDIR(st.st_mode);
}

//遍历文件夹de递归函数
void List_Files_Core(const char *path, int recursive)
{
DIR *pdir;
struct dirent *pdirent;
char temp[256];
pdir = opendir(path);
char new_temp[256];
char buffer[500];
int i=1;
if(pdir)
{
while(pdirent = readdir(pdir))
{
//跳过"."和".."
if(strcmp(pdirent->d_name, ".") == 0
|| strcmp(pdirent->d_name, "..") == 0)
continue;

sprintf(temp, "%s/%s", path, pdirent->d_name);
printf("%s\n", temp);
// sprintf(new_temp,"%s/%d",pdirent->d_name,i++);
// rename(temp,new_temp);
// printf("%s\n",new_temp);
sprintf(buffer,"%d",i++);
rename(pdirent->d_name,buffer);
sprintf(new_temp,"%s/%s",path,pdirent->d_name);
printf("%s\n",new_temp);
//当temp为目录并且recursive为1的时候递归处理子目录
if(IS_DIR(temp) && recursive)
{
List_Files_Core(temp, recursive);
}
}
}
else
{
printf("opendir error:%s\n", path);
}
closedir(pdir);

}
//遍历文件夹的驱动函数
void List_Files(const char *path, int recursive)
{
int len;
char temp[256];

//去掉末尾的'/'
len = strlen(path);
strcpy(temp, path);
if(temp[len - 1] == '/') temp[len -1] = '\0';

if(IS_DIR(temp))
{
//处理目录
List_Files_Core(temp, recursive);
}
else   //输出文件
{
printf("%s\n", path);
}
}

int main(int argc, char** argv)
{
if(argc != 2)
{
printf("Usage: ./program absolutePath\n");
exit(0);
}

List_Files(argv[1], 1);
return 0;
}

这个代码是参考别人的,但是我记不清来源的网址,在此表示道歉

这个我还没有实现我想要的功能,我想实现的就是简单的读取所有指定文件下的所有文件并且将他们改名字,我做这个的目的是因为我收集了很多暴力音频的文件可是分布在不同的文件夹下,我想统一copy出来直接改名字改成i++的型的这样方便成为training data,这样我在SVM下处理就很方便

等实现后我在把代码粘上去

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

修改过后的程序,基本上可以满足我的要求但是还是有点缺陷,不能把文件复制出来,只能连着文件夹一起复制,还的继续修改

#include <stdio.h>
#include <stdlib.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>

//char location_foder[256];
//建立一个文件夹存取内容
char *creat_foder()
{
char *location_foder = "/home/zf/zf/1";
mkdir(location_foder,S_IRUSR|S_IWUSR);
//printf("%s\n",location_foder);
return location_foder;

}

//copy内容
/*void copy_content()
{
char *location_foder;
char file_path[222];
strcpy(file_path,creat_foder());
//system("cp /home/zf/zf/1.c /file_path");
//printf("%s\n%s\n",file_path,creat_foder());
char new_temp[256];
char new_name[10];
int i=0;
sprintf(new_name,"%d%s",i++,".wav");
sprintf(new_temp,"%s/%s",file_path,new_name);
extern int errno;
char old_name[50];
if(rename(old_name,new_temp)==0)
{
printf("Moved OK!\n");
}
else
{
printf("cannot moved!\n");
printf("errno: %d\n",errno);
printf("ERO :%s\n",strerror(errno));
}
}*/
//判断是否为目录
int IS_DIR(const char* path)
{
struct stat st;
lstat(path, &st);
return S_ISDIR(st.st_mode);
}

//遍历文件夹的递归函数
void List_Files_Core(const char *path, int recursive)
{
DIR *pdir;
struct dirent *pdirent;
char temp[256];
pdir = opendir(path);
char new_temp[256];
char buff[10];
char mv_buff[10];
int i=1,j=1;
char mv_temp[256];
if(pdir)
{
while(pdirent = readdir(pdir))
{
//跳过"."和".."
if(strcmp(pdirent->d_name, ".") == 0
|| strcmp(pdirent->d_name, "..") == 0)
continue;

sprintf(temp, "%s/%s", path, pdirent->d_name);
// printf("%s\n", temp);
printf("%s\n%s\n",path,pdirent->d_name);

sprintf(buff,"%d%s",i++,".wav");
sprintf(mv_buff,"%d%s",j++,".wav");
strcpy(pdirent->d_name,buff);
printf("%s\n",pdirent->d_name);
sprintf(new_temp,"%s/%s",path,pdirent->d_name);
printf("%s\n",new_temp);
sprintf(mv_temp,"%s/%s",creat_foder(),mv_buff);
printf("%s\n",mv_temp);
/*extern int errno;
if(rename(new_temp,mv_temp)==0)
{
printf("OK\n");
}
else
{
printf("cannot move!\n");
printf("errno :%d\n",errno);
printf("ERO: %s\n",strerror(errno));
}*/
//rename(new_temp,"/home/zf/zf/1");
// sprintf(new_temp,"%s/%d",pdirent->d_name,i++);
// rename(temp,new_temp);
// printf("%s\n",new_temp);
//sprintf(buffer,"%d",i++);
//rename(pdirent->d_name,buffer);
//sprintf(new_temp,"%s/%s",path,pdirent->d_name);
//printf("%s\n",new_temp);
//rename(pdirent->d_name,"zxczxc");
//sprintf(new_temp,"%s/%s",path,pdirent->d_name);
//printf("%s\n",new_temp);
//当temp为目录并且recursive为1的时候递归处理子目录
if(IS_DIR(temp) && recursive)
{
List_Files_Core(temp, recursive);
}
}
}
else
{
printf("opendir error:%s\n", path);
}
closedir(pdir);

}
//遍历文件夹的驱动函数
void List_Files(const char *path, int recursive)
{
int len;
char temp[256];

//去掉末尾的'/'
len = strlen(path);
strcpy(temp, path);
if(temp[len - 1] == '/') temp[len -1] = '\0';

if(IS_DIR(temp))
{
//处理目录
List_Files_Core(temp, recursive);
}
else //输出文件
{
printf("%s\n", path);
}
}

int main(int argc, char** argv)
{
//char location_foder[256];
//printf("please input the location:\n");
//gets(location_foder);
creat_foder();
// copy_content();
if(argc != 2)
{
printf("Usage: ./program absolutePath\n");
exit(0);
}

List_Files(argv[1], 1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CC++