当前位置: 技术问答>linux和unix
Linux下怎样遍历整个目录文件?(分不够,绝对加!)
来源: 互联网 发布时间:2014-12-12
本文导语: 对于Linux下的某个目录,我想遍历这个目录中的所有文件,并且得到每个文件的属性(例如:创建、修改时间,大小等)。注意:目录中可能有子目录!希望能有源代码!先谢谢!我得Email:fuyuan_hu@sina.com ...
对于Linux下的某个目录,我想遍历这个目录中的所有文件,并且得到每个文件的属性(例如:创建、修改时间,大小等)。注意:目录中可能有子目录!希望能有源代码!先谢谢!我得Email:fuyuan_hu@sina.com
|
void check_dir(char* dirname)
{
DIR* p;
struct dirent* dirlist;
struct stat filestat;
char indir[1024];
p=opendir(dirname);
while((dirlist=readdir(p))!=NULL)
{
sprintf(indir,"%s/%s",dirname,dirlist->d_name);
stat(indir,&filestat);
if(S_ISREG(filestat.st_mode))
{
printf("%sn",indir);
}
else
{
if(S_ISDIR(filestat.st_mode)&&dirlist->d_name[0]!='.')
{
check_dir(indir);
}
}
}
}
{
DIR* p;
struct dirent* dirlist;
struct stat filestat;
char indir[1024];
p=opendir(dirname);
while((dirlist=readdir(p))!=NULL)
{
sprintf(indir,"%s/%s",dirname,dirlist->d_name);
stat(indir,&filestat);
if(S_ISREG(filestat.st_mode))
{
printf("%sn",indir);
}
else
{
if(S_ISDIR(filestat.st_mode)&&dirlist->d_name[0]!='.')
{
check_dir(indir);
}
}
}
}
|
#include
#include
#include
void do_search_dir(char *path) {
DIR *dir;
char fullpath[1024],currfile[1024];
struct dirent *s_dir;
struct stat file_stat;
strcpy(fullpath,path);
dir=opendir(fullpath);
while ((s_dir=readdir(dir))!=NULL) {
if ((strcmp(s_dir->d_name,".")==0)||(strcmp(s_dir->d_name,"..")==0)) continue;
sprintf(currfile,"%s/%s",fullpath,s_dir->d_name);
stat(currfile,&file_stat);
if (S_ISDIR(file_stat.st_mode))
do_search_dir(currfile);
else
printf("%sn",currfile);
}
closedir(dir);
}
int main(int argc,char **argv) {
do_search_dir(argv[1]);
}
注:上面的程序是随手写的,有可能有一些bug和错误。但框架基本是这样的。
#include
#include
void do_search_dir(char *path) {
DIR *dir;
char fullpath[1024],currfile[1024];
struct dirent *s_dir;
struct stat file_stat;
strcpy(fullpath,path);
dir=opendir(fullpath);
while ((s_dir=readdir(dir))!=NULL) {
if ((strcmp(s_dir->d_name,".")==0)||(strcmp(s_dir->d_name,"..")==0)) continue;
sprintf(currfile,"%s/%s",fullpath,s_dir->d_name);
stat(currfile,&file_stat);
if (S_ISDIR(file_stat.st_mode))
do_search_dir(currfile);
else
printf("%sn",currfile);
}
closedir(dir);
}
int main(int argc,char **argv) {
do_search_dir(argv[1]);
}
注:上面的程序是随手写的,有可能有一些bug和错误。但框架基本是这样的。
|
opendir()
readdir()
closedir()
stat()
递归执行.
readdir()
closedir()
stat()
递归执行.
|
/*这是我的删除目录及其子目录下的文件的函数,曾在红帽linux下通过,看是否能帮上你*/
#include
#include
#define DWORD unsigned short;
#define BYTE unsigned char;
DWORD API_DelDir(BYTE *Path)
{
DIR *dirp;
struct dirent *dp;
BYTE cPath[256];
struct stat pStat;
if((dirp = opendir(Path)) == NULL)
{
printf("The dir %s is not exit!n",Path);
return SUCCESS;
}
while ((dp = readdir( dirp )) != NULL )
{
if ((strcmp(dp->d_name,".")==0)||(strcmp(dp->d_name,"..")==0))
continue;
strcpy(cPath,Path);
strcat(cPath,"/");
strcat(cPath,dp->d_name);
if (stat(cPath,&pStat) == -1)
{
if(GV_ucDebug)
{
perror("stat:");
printf("get stat %s err!n",cPath);
}
closedir(dirp);
return FAILURE;
}
if ((pStat.st_mode & S_IFMT) == S_IFDIR)
{
if(API_DelDir(cPath)!=OMC_SUCCESS)
{
closedir(dirp);
return FAILURE;
}
}
else
{
if (unlink(cPath)==-1)
{
perror("unlink");
printf("%s n",cPath);
closedir(dirp);
return FAILURE;
}
}
}
closedir(dirp);
if (rmdir (Path) ==-1)
{
if(GV_ucDebug)
{
perror ("rmdir");
printf(" %s n",Path);
}
return FAILURE;
}
return SUCCESS;
}/*End Func */
#include
#include
#define DWORD unsigned short;
#define BYTE unsigned char;
DWORD API_DelDir(BYTE *Path)
{
DIR *dirp;
struct dirent *dp;
BYTE cPath[256];
struct stat pStat;
if((dirp = opendir(Path)) == NULL)
{
printf("The dir %s is not exit!n",Path);
return SUCCESS;
}
while ((dp = readdir( dirp )) != NULL )
{
if ((strcmp(dp->d_name,".")==0)||(strcmp(dp->d_name,"..")==0))
continue;
strcpy(cPath,Path);
strcat(cPath,"/");
strcat(cPath,dp->d_name);
if (stat(cPath,&pStat) == -1)
{
if(GV_ucDebug)
{
perror("stat:");
printf("get stat %s err!n",cPath);
}
closedir(dirp);
return FAILURE;
}
if ((pStat.st_mode & S_IFMT) == S_IFDIR)
{
if(API_DelDir(cPath)!=OMC_SUCCESS)
{
closedir(dirp);
return FAILURE;
}
}
else
{
if (unlink(cPath)==-1)
{
perror("unlink");
printf("%s n",cPath);
closedir(dirp);
return FAILURE;
}
}
}
closedir(dirp);
if (rmdir (Path) ==-1)
{
if(GV_ucDebug)
{
perror ("rmdir");
printf(" %s n",Path);
}
return FAILURE;
}
return SUCCESS;
}/*End Func */
|
一个简单的仿ls程序:
#include
#include
int main(int argc, char *argv[])
{
DIR *mydir;
struct dirent *mydirp;
if ( argc != 2 )
printf("请带一个目录再执行");
if (( mydir = opendir( argv[1] )) ==NULL )
{
printf("can't open %s", argv[1]);
exit(1);
}
while (( mydirp = readdir( mydir )) != NULL )
printf("%sn", mydirp->d_name);
closedir( mydir );
exit(0);
}
#include
#include
int main(int argc, char *argv[])
{
DIR *mydir;
struct dirent *mydirp;
if ( argc != 2 )
printf("请带一个目录再执行");
if (( mydir = opendir( argv[1] )) ==NULL )
{
printf("can't open %s", argv[1]);
exit(1);
}
while (( mydirp = readdir( mydir )) != NULL )
printf("%sn", mydirp->d_name);
closedir( mydir );
exit(0);
}
|
我是用PHP写的,为了生成资源管理器式的web界面用的.
所是echo 出来很多是javascript的涵数,
从算法来讲这应该算一个先序遍历吧.
所是echo 出来很多是javascript的涵数,
从算法来讲这应该算一个先序遍历吧.