易码技术论坛

 找回密码
 加入易码
搜索
查看: 483487|回复: 11

[源码] 遍历指定目录下文件名

[复制链接]
发表于 2006-12-8 20:40:55 | 显示全部楼层
用的标准的C的库?
发表于 2006-12-8 20:43:48 | 显示全部楼层
不推荐用带_的函数
还是用api吧
 楼主| 发表于 2006-12-8 21:41:54 | 显示全部楼层
我还确实不清楚是不是标准C库里面的,而且这一系列的函数都是以“_”开头的。
发表于 2006-12-8 22:52:08 | 显示全部楼层
来个JAVA版本的吧,支持正则式:
  1. import java.io.*;
  2. import java.util.regex.*;
  3. /**
  4. *@author Eastsun
  5. *@version 1.0 12/8/2006
  6. */
  7. public class SearchFile{
  8.    /**
  9.    *@param dir 搜索进行的根目录
  10.    *@param p 文件名匹配的模式
  11.    *@param out 负责输出搜索结果PrintStream
  12.    */
  13.    public static int searchFile(File dir,Pattern p,PrintStream out)throws IOException{
  14.       int count =0;
  15.       if(dir.isDirectory()){
  16.         File[] list =dir.listFiles();
  17.         if(list==null) return 0;
  18.         for(int n=0;n<list.length;n++){
  19.            if(list[n].isDirectory()) count +=searchFile(list[n],p,out);
  20.            else if(p.matcher(list[n].getName()).matches()){
  21.               out.println(list[n].getCanonicalPath());
  22.               count++;
  23.            }
  24.         }
  25.       }
  26.       return count;
  27.    }
  28.    public static void main(String[] args)throws IOException{
  29.       File root =null;                          
  30.       Pattern p =null;                  
  31.       PrintStream out =null;            
  32.       
  33.       if(args.length==3){
  34.         root =new File(args[0]);              //搜索的根目录
  35.         p =Pattern.compile(args[1]);           //匹配模式
  36.         out =new PrintStream(new File(args[2]));   //将搜索结果保存到文件名为args[2]的文件中
  37.       }
  38.       else{
  39.         root =new File("e:\");        //搜索的根目录为e:盘
  40.         p = Pattern.compile(".*\\.lav");  //搜索所有的lav文件
  41.         out =System.out;            //直接在控制台上显示搜索结果
  42.       }
  43.       System.out.println("Searching..");
  44.       int count =searchFile(root,p,out);
  45.       System.out.println("Find "+count+" files");
  46.    }
  47. }
复制代码
发表于 2006-12-8 22:55:40 | 显示全部楼层
匹配所有文件名的正则式是: Pattern p =Pattern.compile(".+");
 楼主| 发表于 2006-12-9 00:35:17 | 显示全部楼层
递归啊递归,不过感觉listFile函数有些无耻...
发表于 2006-12-9 13:08:08 | 显示全部楼层
LZ的代码貌似只把C盘根目录下的文件打印出来,而不是把C盘所有的文件打印出来?
 楼主| 发表于 2006-12-10 11:02:32 | 显示全部楼层
对,我说了是C盘根目录的,没有子目录的递归。
发表于 2006-12-21 18:09:29 | 显示全部楼层
JAY,一进来,第一眼就看到你了!

我还是第一次来这里,但是却一下载看到好多熟悉的名字,Lee(leesoft就是Lee本人吗?),FantasyDR,LOJA,曾半仙,任我狂
又记起当初的文曲星年代了,响起当时天天上课扒在桌子上汇编,LAVA的情景了~

顶个~
发表于 2006-12-21 18:34:21 | 显示全部楼层
leesoft就是Lee
这里不让注册Lee,真不爽
发表于 2006-12-21 18:52:15 | 显示全部楼层
--||原来还有这么一回事。。。。。。
 楼主| 发表于 2006-12-8 19:25:39 | 显示全部楼层 |阅读模式
使用_findfirst, _findnext, _findclose三个函数可以遍历指定目录下的所有文件,然后通过判断文件的属性(文档,目录,系统,只读,隐藏,普通)来进行相应的操作,下面这个代码就可以打印出C盘根目录下的所有文件。
  1. #include <stdio.h>
  2. #include <io.h>
  3. void main()
  4. {
  5.    intptr_t findHandle; // 查找文件句柄
  6.    char fileSpec[] = "C:\\*.*"; // 查找文件条件
  7.    struct _finddata_t fileInfo; // 文件信息
  8.    findHandle = _findfirst(fileSpec, &fileInfo); // 开始查找
  9.    if (findHandle != -1) // 找到文件
  10.    {
  11.       printf("Files found below:\n");
  12.       do
  13.       {
  14.          if (fileInfo.attrib & _A_ARCH) // 如果是文档,输出文件名
  15.         {
  16.             printf("%s\n", fileInfo.name);
  17.         }
  18.       }while(_findnext(findHandle, &fileInfo) != -1); // 查找下一个文件
  19.    }
  20.    else
  21.    {
  22.         printf("No file found\n"); // 没有找到任何文件
  23.    }
  24.    _findclose(findHandle); // 关闭查找
  25. }
复制代码
您需要登录后才可以回帖 登录 | 加入易码

本版积分规则

Archiver|手机版|小黑屋|EMAX Studio

GMT+8, 2024-3-29 02:59 , Processed in 0.010572 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表