【app】遍历目录所有文件
遍历目录所有文件
原创,转载时请注明,谢谢。邮箱:tangzhongp@163.com
博客园地址:http://www.cnblogs.com/embedded-tzp
Csdn博客地址:http://blog.csdn.net/xiayulewa
Linux C : readdir
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
int main(){
DIR *dir_p = opendir("/");
if(dir_p == NULL) perror("opendir"), exit(-1);
struct dirent *ent;
while(1){
ent = readdir(dir_p);
if(ent == NULL) break;
//打印子项类型和子项名
if( 0 == strcmp(ent->d_name, ".")
|| 0 == strcmp(ent->d_name, "..")){
continue;
}
printf("%d, %s\n", ent->d_type, ent->d_name);
//type == 4 是目录,其他是文件
}
}
Linux C: scandir
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int dir_printall(const char *pathname)
{
struct dirent **namelist = NULL;
int ent_n;
int i;
ent_n = scandir(pathname, &namelist, NULL, alphasort);
if (ent_n < 0){
printf("scandir() fail : %s\n", pathname);
return -1;
}
for(i = 0; i < ent_n; i++){
if( 0 == strcmp(namelist[i]->d_name, ".")
|| 0 == strcmp(namelist[i]->d_name, "..")){ // skip parent dir and self
continue;
}
char path_buf[512] = {0}; // use malloc will be bettor
sprintf(path_buf, "%s/%s", pathname, namelist[i]->d_name);
printf("%s\n", path_buf);
if(4 == namelist[i]->d_type){ // 4 means dir
int retval = dir_printall( path_buf); // recurrence call
if(-1 == retval){
fprintf(stderr, "dir_printall() fail: %s\n", path_buf);
continue;
//goto out; // not end, for /proc/5236/net can't
}
}
}
out:
for(i = 0; i < ent_n; i++){
if(namelist[i]){
free(namelist[i]);
namelist[i] = NULL;
}
}
if(namelist){
free(namelist);
namelist = NULL;
}
return 0;
}
int main(void)
{
if (-1 == dir_printall("/")){
perror("dir_printall()");
}
return 0;
}
C++
shell
#带完整路径
file=$(find ./)
echo $file
#只有文件名
file=$(ls -R)
echo $file
qt
自己做的项目中拷贝出来的一段简化的代码。
bool SearchThread::createDb()
{
qDebug() << __LINE__ << __FUNCTION__;
QFileInfoList fileDriveList = QDir::drives(); // 获取盘符,windows下为c:, d:, e:, linux下为 /
foreach(QFileInfo fileDrive, fileDriveList){ // 循环处理盘符
qDebug() << fileDrive.absoluteFilePath();
createDb(fileDrive.absoluteFilePath());
}
return true;
}
bool SearchThread::createDb(const QString &filePath)
{
QDir dir = filePath;
const QDir::Filters FILTERS = QDir::AllDirs | QDir::Files | QDir::Drives
| QDir::NoDotAndDotDot | QDir::Hidden | QDir::System;
QFileInfoList fileInfoList = dir.entryInfoList(FILTERS, QDir::DirsFirst | QDir::Name);
foreach(QFileInfo fileInfo, fileInfoList){
bool isdir = fileInfo.isDir();
if(isdir){
if(!fileInfo.isSymLink()){ // 不是链接文件,防止死循环
createDb(fileInfo.absoluteFilePath());
}
}
}
return true;
}
【app】遍历目录所有文件的更多相关文章
- Linux下遍历目录及文件,更改权限
Linux下遍历目录及文件,更改权限 引言: 我在Linux下搭建android时,将eclipse及sdk复制到/usr/下时,总会出现无法读,无法写写样的问题. 解决方案: 有两个方案: 一.将复 ...
- php遍历目录下文件,并读取内容
<?php echo "<h2>遍历目录下文件,并读取内容</h2><br>\n"; function listDir($dir) { i ...
- dos下遍历目录和文件的代码(主要利用for命令)
对指定路径指定文件进行遍历的程序,这里有多个批处理代码,但运行好像有些问题,大家可以根据需要选择 ===== 文件夹结构 ======================================= ...
- File类遍历目录及文件
1. 构造函数 File(String args0)//使用一个表示文件或目录的路径的字符串创建一个File对象 File(URL args0)//使用一个URL对象创建File对象 File(Fil ...
- dos下遍历目录和文件的代码(主要利用for命令)(转)
===== 文件夹结构 ============================================= D:\test ---A Folder 1 |-----A file 1.txt | ...
- php遍历目录与文件夹的多种方法详解
遍历目录或遍历目录下指定类型的文件,这是每一个童鞋在写程序的时候难免会用到的.PHP本身也提供了很多灰常有用的函数,正确地使用它们,不会有错滴.下面就我个人学习过程中的一些总结,希望对想学PHP的童鞋 ...
- Win32下C++遍历目录和文件的源码
#include<windows.h> #include<iostream> #include<string> using namespace std; //只能处 ...
- PHP遍历目录和文件及子目录和文件
正常直接使用opendir方法,就可以读到所有的目录和文件 文件可以直接记录下来,目录则需要再进一步获取里边的文件信息 也就是,如果当前读出来是目录,则需要再次调用函数本身(递归),直到没有目录 循环 ...
- linux c 遍历目录及文件
#include <dirent.h>void recovery_backend() { DIR * pdir ; struct dirent * pdirent; struct stat ...
随机推荐
- [转][Swust OJ 24]--Max Area(画图分析)
转载自:http://www.cnblogs.com/hate13/p/4160751.html Max Area 题目描述:(链接:http://acm.swust.edu.cn/problem/2 ...
- 如何借助Motion操控Linux监控摄像头
介绍 本文介绍如何使用motion来操控Linux下的摄像头. 安装 apt-get install motion 配置文件 输入命令后面的命令编辑配置文件, vim /etc/motion/moti ...
- JAVA的一些小笔记
构造块优先于构造方法执行,而且每当有一个新的实例化对象产生时,就会重复执行构造块的程序. 静态块优先于构造块执行,而且不管有多少个实例化对象产生,静态块只会执行一次,它的主要作用是为类中的static ...
- hadoop安全模式
hadoop安全模式在分布式文件系统启动的时候,开始的时候会有安全模式,当分布式文件系统处于安全模式的情况下,文件系统中的内容不允许修改也不允许删除,直到安全模式结束.安全模式主要是为了系统启动的 ...
- [转]Centos6.5使用yum安装mysql—配置MySQL允许远程登录
一.mysql安装 第1步.yum安装mysql[root@stonex ~]# yum -y install mysql-server安装结果:Installed: mysql-server ...
- redis 消息队列(发布订阅)、持久化(RDB、AOF)、集群(cluster)
一:订阅: 192.168.10.205:6379> SUBSCRIBE test Reading messages... (press Ctrl-C to quit) 1) "sub ...
- 基于visual Studio2013解决C语言竞赛题之0906文件插入
题目
- leetcode 编辑距离
class Solution { public: int minDistance(string word1, string word2) { // Start typing your C/C++ so ...
- C# char[]与string之间的相互转换
string 兑换 Char[] string ss = "abcdefg"; char[] cc = ss.ToCharArray(); Char[] 转换成string str ...
- TortoiseSVN 文件关联图标不显示的解决方法
对于SVN来说,因为每个图标都代表着不同的含义,预示着不同的状态,是指示灯的作用,如果没有正确的图标很可能造成数据的丢失等 之前看了网上其他人写的帖子,,有一些是直接删除注册表下“ShellIconO ...