Chinaunix首页 | 论坛 | 博客
  • 博客访问: 90335622
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: C/C++

2008-05-25 10:48:54

作者: opius     出自:
用opendir打开一个目录,得到结构DIR*,但是没有关于其下有多少文件、子目录的数据,我用比较土的办法,遍历目录readdir,计算其下有多少文件和子目录,当然也不是连子目录下的东西也找。

下面的程序在solaris8、gcc编译通过的,如果一个目录是空的,输出为2。
#include
#include
#include
int main(int argc , char **argv)
{
DIR *dirp;
int num=0;

dirp = opendir(argv[1]);
while (dirp) {
if ( readdir(dirp) != NULL)
++num;
else
break;
}

closedir(dirp);
printf("%d\n",num);
}

shell中判断目录为空
#!/bin/ksh
# Check if a directory is empty or not

if [ $# = 0 ]
then
echo
echo "use this tool to check if directory is empty"
echo "for example: isEmpty dirName "
echo
exit 1
fi

case $(( 0 + $(find $1 2>&- |head -2|wc -l))) in

0) echo Permission denied ! ;;
1) echo Directory is empty ! ;;
*) echo Directory is not empty ! ;;

esac

阅读(3265) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~