递归列出目录下文件的全路径

本程序是在网络搜索到的目录遍历用法加以修改,增加功能实现的.所以欢迎转载使用.
/*
由于ls -R,递归目录列出的信息格式在需要全路径时不好处理,因此专门写这一程序.
linux下编译:  gcc lsdir.c -o lsdir -O
windows下编译:使用codeblocks即可.
Useage: lsdir
list the full path of a dir's subdirs or files,not same as  "ls -R".
-h,    print this message.
-d dirname,    recursely print full path of files.
-D,    print dirs only.
-F,    print files only.
-A,    print files and dirs(defult).
-R,    print dir recurse(defult).
-r,    only print current dir.
none argvs,    print current path.
---zhouchuan,2010.8.27.
*/
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#ifdef __WIN32__  // OS_COMPAT
//__WINDOWS__
char path_sep = '\';
#else
char path_sep = '/';
#endif   // !__WINDOWS__

#define DIR_MAX_LEN 1000
#define PRINT_DIR_ONLY 0
#define PRINT_FILE_ONLY 1
#define PRINT_FILE_AND_DIR 2

#define DIR_RECURSE_NO 0
#define DIR_RECURSE_YES  1

int g_PRINT_TYPE = PRINT_FILE_AND_DIR; //默认列出所有目录及文件
int g_DIR_RECURSE = DIR_RECURSE_YES;//默认递归

void printdir(char *dir, int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
char  buff[DIR_MAX_LEN];


if((dp = opendir(dir)) == NULL) {
fprintf(stderr, "cannot open directory: %s ", dir);
return;
}
chdir(dir);
getcwd(buff,DIR_MAX_LEN);
while((entry = readdir(dp)) != NULL) {
//lstat(entry-> d_name,&statbuf);
stat(entry-> d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)) {
/* Found a directory, but ignore . and .. */
if(strcmp( ".",entry-> d_name) == 0 ||strcmp( "..",entry-> d_name) == 0)
continue;
// printf( "%*s%s/ ",depth, " ",entry-> d_name);
if(g_PRINT_TYPE != PRINT_FILE_ONLY)
printf( "%s%c%s%c ",buff,path_sep,entry-> d_name,path_sep);

/* Recurse at a new indent level */
if(g_DIR_RECURSE==DIR_RECURSE_YES)
printdir(entry-> d_name,depth+4);
}else{
//is a file
if(g_PRINT_TYPE !=PRINT_DIR_ONLY){
printf( "%s%c%s ",buff,path_sep,entry-> d_name);
}
}
}
chdir( "..");
closedir(dp);
}

void useage(){
printf("==================================================================== ");
printf("Useage: lsdir ");
printf(" list the full path of a dir's subdirs or files,not same as  "ls -R". ");
printf(" -h, print this message. ");
printf(" -d dirname, recursely print full path of files. ");
printf(" -D, print dirs only. ");
printf(" -F, print files only. ");
printf(" -A, print files and dirs(defult). ");
printf(" -R, print dir recurse(defult). ");
printf(" -r, only print current dir. ");
printf(" none argvs, print current path. ");
printf(" ---zhouchuan,2010.8.27. ");
printf("==================================================================== ");
}

int main(int argc, char* argv[])
{
int oc;                     /*选项字符 */
char *b_opt_arg;            /*选项参数字串 */
char *topdir=NULL, pwd[2]= ".";

while((oc = getopt(argc, argv, "hd:DFARr")) != -1)
{
switch(oc)
{
case 'h':
//-h,则打印使用方法
useage();
return 0;
break;
case 'D':
g_PRINT_TYPE =PRINT_DIR_ONLY ;
break ;
case 'F':
g_PRINT_TYPE =PRINT_FILE_ONLY ;
break ;
case 'A':
g_PRINT_TYPE =PRINT_FILE_AND_DIR ;
break ;
case 'R':
g_DIR_RECURSE = DIR_RECURSE_YES;
//       printf(" DIR_RECURSE_YES");
break;
case 'r':
g_DIR_RECURSE = DIR_RECURSE_NO;
//    printf(" DIR_RECURSE_NO");
break;
case 'd':
b_opt_arg = optarg;
//      printf("dir %s ", optarg);
topdir=optarg;
break;
}
}
if(topdir==NULL)
{
//   printf("dir current");
topdir=pwd;
}
printf("Directory scan of "%s": ",topdir);
printdir(topdir,0);
printf("===Done. ");

return 0;
}

在很多时候,我们可能需要得到一个目录下所有文件的全路径,以供后续计算.
如要计算当前目录所有文件的md5值:


./lsdir -F|grep "/"|while read LINE;do md5sum $LINE;done;

源码: lsdir

| 296个评论