example1 扫描当前文件夹下的文件并打印
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
char buff[512];
void print_dir(char *path, int depth)
{
struct dirent **name_list;
int n = scandir(path, &name_list, 0, alphasort);
if(n < 0)
printf( "scandir return %d \n",n);
else
{
int index=0;
while(index < n)
{
sprintf(buff, "name %s/%s", path, name_list[index]->d_name);
printf("%s\n", buff);
free(name_list[index++]);
}
free(name_list);
}
}
int main(int argc, char* argv[])
{
char *now_dir, pwd[2]=".";
if (argc != 2)
now_dir = pwd;
else
now_dir = argv[1];
printf("Directory scan of %s\n",now_dir);
print_dir(now_dir,0);
printf("Finish.\n");
exit(0);
}
example2 扫描当前文件夹下的文件以及子目录的所有文件及文件类型并打印
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
void scan_dir(char *dir, int depth)
{
DIR* dp;
struct dirent* entry;
struct stat statbuf;
if (NULL == (dp = opendir(dir)))
{
fprintf(stderr, "Can`t open directory %s\n", dir);
return ;
}
chdir(dir);
while (NULL != (entry = readdir(dp)))
{
lstat(entry->d_name, &statbuf);
if (S_ISDIR(statbuf.st_mode))
{
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
printf("%*s%s/ 【file type: %s】\n", depth, "", entry->d_name, "directory file");
scan_dir(entry->d_name, depth+4);
}
else
{
if(S_ISREG(statbuf.st_mode))
printf("%*s%s 【file type: %s】\n", depth, "", entry->d_name, "regular file");
else if (S_ISCHR(statbuf.st_mode))
printf("%*s%s 【file type: %s】\n", depth, "", entry->d_name, "character special file");
else if (S_ISBLK(statbuf.st_mode))
printf("%*s%s 【file type: %s】\n", depth, "", entry->d_name, "block special file");
else if (S_ISFIFO(statbuf.st_mode))
printf("%*s%s 【file type: %s】\n", depth, "", entry->d_name, "fifo file");
}
}
chdir("..");
closedir(dp);
}
int main(void)
{
char topdir[32] = {0};
printf("plese input scan of Directory: ");
scanf("%s", topdir);
printf("Directory scan of %s\n", topdir);
scan_dir(topdir, 0);
printf("done.\n");
return 0;
}
example3 扫描当前文件夹下的文件输出文件类型及绝对路径并打印
#include <stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<dirent.h>
#include<sys/types.h>
#include <string.h>
#include <sys/stat.h>
int getdir(char * pathname)
{
DIR* path=NULL;
path=opendir(pathname);
if(path==NULL)
{
perror("failed");
exit(1);
}
struct dirent* ptr;
char buf[1024]={0};
while ((ptr=readdir(path))!=NULL)
{
if(strcmp(ptr->d_name,".")==0||strcmp(ptr->d_name,"..")==0)
{
continue;
}
if(ptr->d_type==DT_DIR)
{
sprintf(buf,"%s/%s",pathname, ptr->d_name);
printf("目录:%s\n",buf);
getdir(buf);
}
if (ptr->d_type==DT_REG)
{
sprintf(buf,"%s/%s",pathname,ptr->d_name);
printf("文件:%s\n",buf);
}
}
return 0;
}
#define DIR_PATH "/opt/file_list"
int main()
{
int ret;
DIR * mydir = NULL;
if ((mydir = opendir(DIR_PATH)) == NULL)
{
ret = mkdir(DIR_PATH, 0755);
if (ret != 0)
{
printf("%s created erro!\n", DIR_PATH);
return -1;
}
printf("%s created sucess!\n", DIR_PATH);
}
else
{
printf("%s exist!\n", DIR_PATH);
}
getdir(DIR_PATH);
return 0;
}