如何读取所有文件夹下的所有文件
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;
}
相关文章推荐
- 如何读取一个文件夹下的所有图像文件,并制作负样本到指定目录
- 如何读取文件夹下的所有文件名到TXT文件中
- 【python】如何批量读取文件夹的所有文件数据,os模块与open函数结合使用实例
- .net中如何读取服务器上指定文件夹中的所有文件 (百度知道 )
- python如何读取文件夹下的所有文件
- cocos2d-x 如何读取遍历文件夹下的所有文件
- 如何读取某个指定文件夹下所有文件,并筛选出其中的图片文件(jpeg,bmp)展示出来。
- 在网上找了一个读取文件夹下面的所有文件到txt的code 已验证
- java读取文件夹下所有文件并替换文件每一行中指定的字符串
- [R语言]读取文件夹下所有子文件夹中的excel文件,并根据分类合并。
- scala 递归读取文件夹下所有的指定后缀的文件
- 遍历读取指定文件夹下指定类型的所有文件
- Python如何读取指定文件夹下的所有图像
- Java中读取某个目录下的所有文件和文件夹
- Windows下如何用C语言清空特定文件夹中的所有文件
- C++读取文件夹中所有的文件或者是特定后缀的文件
- 遍历读取指定文件夹下指定类型的所有文件
- 如何读取assets文件夹中的txt文件
- matlab一次读取多幅图片的方法////////////读取文件夹中的所有图片or文件