【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 ...
随机推荐
- centos 安装lua
yum install readline-develwget http://www.lua.org/ftp/lua-5.1.4.tar.gztar -xzvf lua-5.1.4.tar.gz3.编译 ...
- 根据Email地址跳转到相应的邮箱登录页面 (转)
//跳转到指定的邮箱登录页面 $(".btn_actemail").click(function () { var uurl = $(".hide_email" ...
- LGPL与闭源程序
最近一直在学习 Qt.Qt 有两个许可证:LGPL 和商业协议.这两个协议在现在的 Qt 版本中的代码是完全一致的(潜在含义是,Qt 的早期版本,商业版的 Qt 通常包含有一些开源版本所没有的库,比如 ...
- 基于Sql Server 2008的分布式数据库的实践(五)
原文 基于Sql Server 2008的分布式数据库的实践(五) 程序设计 ------------------------------------------------------------- ...
- 利用 html的锚点(元素a)功能实现ajax单页面应用的浏览器后退前进功能
一.问题 随着AJax技术的普及,单页面web程序的应用越来越广泛. 所谓单页面应用程序,简单的说,就是应用只有一个主网页,第一次加载后,后续页面只会利用js和ajax到服务器获取数据进行页面的局部刷 ...
- bootstrap scaffold框架
这是一段典型的html typical HTML file: <!DOCTYPE html> <html> <head> <title>Bootstra ...
- 17.1.1.4 Obtaining the Replication Master Binary Log Coordinates 获取复制Master Binary Log的坐标:
17.1.1.4 Obtaining the Replication Master Binary Log Coordinates 获取复制Master Binary Log的坐标: 你需要master ...
- mojo 默认use utf8;
my $endtime=strftime("%Y%m%d%H%M%S",localtime()); my $d=encode_utf8('验证'); if ($a3 =~/$d/) ...
- 进入MFC讲坛的前言(五)
框窗.视图和文档及其关系 MFC架构的另外一个特色是它的框窗.视图和文档这个三位一体的结构,它是一个典型的MVC(Model.View and Controler)结构.严格的讲,框窗不属于MVC中的 ...
- Http方式获取网络数据
通过以下代码可以根据网址获取网页的html数据,安卓中获取网络数据的时候会用到,而且会用Java中的sax方式解析获取到数据.(sax解析主要是解析xml)具体代码如下: package com.wy ...