中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > 综合其它
Windows下文件夹遍历(2)
作者:未知 时间:2005-07-27 23:19 出处:CSDN 责编:chinaitpower
              摘要:Windows下文件夹遍历(2)

      修改了一下程序,使其可以在当前路径下以文件名“遍历结果.txt”输出运行结果;
    运行结果改为树形输出方式

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <stack>

#include <windows.h>

using namespace std;

/*
* 遍历lpszPath下所有文件及文件夹,并按顺序显示其中的内容.
*/
/*
* 如果扫描到文件夹,则将其存入 Dirs 队列中,并显示名称,
* 如果扫描到文件,则显示其名称;
* 当前文件夹处理完毕后就处理其下一级文件夹,下一级文件夹从队
* 列中得到.
*/

void function( LPCTSTR lpszPath,ostream & out)
{
    //开始查找;
    stack<TCHAR*> Dirs;
    stack<int>    DirDepth;

    TCHAR *tmp=new TCHAR[lstrlen(lpszPath)+1];
    lstrcpy(tmp,lpszPath);

    if(tmp[lstrlen(tmp)-1]=='\\')
        tmp[lstrlen(tmp)-1]='\0';

    TCHAR szFind[MAX_PATH*2];
    TCHAR szFile[MAX_PATH*2];
    TCHAR *curdir;
    int   curdepth=1; //当前文件夹的深度

    Dirs.push(tmp);
    DirDepth.push(curdepth);

    for(;!Dirs.empty();)
    {
        curdir=Dirs.top();
        curdepth=DirDepth.top();
        Dirs.pop();
        DirDepth.pop();

        lstrcpy(szFind,curdir);
        lstrcat(szFind, "\\*.*"); // 找所有文件
        WIN32_FIND_DATA wfd;

        HANDLE hFind = FindFirstFile(szFind, &wfd);

        if (hFind != INVALID_HANDLE_VALUE) // 如果找到
        {
            if (curdepth >1)
                out<<"    ";
            for(int i=1;i<curdepth-1;++i)
                out<<'|'<<"   ";
            out<<'+'<<curdir<<endl;

            do
            {

                if (wfd.cFileName[0] == '.')
                    continue; // 过滤"." ".." 这两个目录

                if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                {
                    wsprintf(szFile, "%s\\%s", curdir, wfd.cFileName);

                    //function(szFile); // 如果找到的是目录,则进入此目录进行递归
                    TCHAR* tmp=new TCHAR[lstrlen(szFile)+2];
                    lstrcpy(tmp,szFile);
                    Dirs.push(tmp);
                    DirDepth.push(curdepth+1);
                }
                else
                {
                    //输出文件名
                    out<<"    ";
                    for(int i=1;i<curdepth;++i)
                        out<<'|'<<"   ";
                    out<<wfd.cFileName<<endl;
                }
        } while (FindNextFile(hFind, &wfd));

    }// if

    delete [] curdir;

    FindClose(hFind); // 关闭查找句柄

}// for()
}

int main(int argc,char *argv[])
{
    ofstream fout("遍历结果.txt");
    if(argc<=1)
    {
        cerr<<endl<<"文件夹遍历,请输入路径:";
        TCHAR path[MAX_PATH];
        cin>>path;
        function(path,fout);
    }
    else
    {
        function(argv[1],fout);
    }

    return 0;
}


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有