VC非递归遍历文件夹总算调试好了 (BY 冷家锋)

//********************************************************
//针对Windows 命令行move 移动文件夹不如人意的地方
//写此小程序
//作者:冷家锋
//时间: 2006-12-27
//********************************************************
//算法描述

//用堆栈,对于一个目录,若有文件直接移动到目的文件夹
//若有子文件夹,则进栈
//对栈顶目录有:
//若有文件直接移动到目的文件夹,有子文件夹则进栈

//进栈操作:栈长度增1,元素进栈
//出栈操作:元素出栈,栈长度减1
//访问两次的文件夹应该出栈
#include <afx.h>
#include <vector>
#include <iostream>

using namespace std;

CStringArray directory;
vector<int>  AccDirCount(1,0);

CString strSrcDir = "D://2//*.*";
CString strDstDir = "c://2//*.*";

int main()
{
   
    //RemoveDirectory("D://1");
   
   
    //CreateDirectory("D://3",NULL);
   
    //栈顶指针
    int top = 0;
    //int iDirCount = 1;
   
    CFileFind ff;
    bool bSuccess = ff.FindFile(strSrcDir);
    if( !bSuccess )
    {
        return 1;
    }
   
    //待拷贝的根文件夹进栈
    directory.SetSize(top+1);
    AccDirCount.resize(top+1);
   
    directory[top] = strSrcDir;
    AccDirCount[top] = 0;
   
   
    int iCount = 0;
    int iSrcDirCount = 0;
   
    while ( top >= 0 )
    {
        CString strTopDir = directory[top];   
       
        int a = AccDirCount[top];
        AccDirCount.pop_back();
        AccDirCount.push_back(a+1);
       
        int ifok = AccDirCount[top];
        //若栈顶文件夹已访问两次,则出栈,重新至栈顶取元素
        if (ifok < 2)
        {
            //在新的目录建立文件夹
            printf("Directory: %s/n", strTopDir);
           
            bSuccess = ff.FindFile(strTopDir);
            if( !bSuccess )
            {
                return 1;
            }
           
            while(bSuccess)
            {
                //开始遍历
                bSuccess = ff.FindNextFile();
               
                //子目录或文件数加1
                CString strFile = ff.GetFilePath();
               
                if( !ff.IsDots() )
                {
                    //子目录
                    if( ff.IsDirectory() )
                    {
                        //子目录进栈
                        CString strTmpDir = ff.GetFilePath();
                       
                        top++;
                        directory.SetSize(top+1);    //top指针比栈长度小1
                       
                        directory[top] = strTmpDir + "//*.*";
                        AccDirCount.push_back(0);
                    }
                    else    //是文件,则移动
                    {
                        CString strTmpFile = ff.GetFileName();
                        printf("File:  %s/n", strTmpFile);
                    }
                }
                else
                {
                    //打印出.和..
                    //CString cc = ff.GetFileName();
                    //printf("%s/n",cc);
                }
            }//while(success)
           
        }
        else
        {
            directory.SetSize(top);    //栈顶元素出栈,删除文件夹
            AccDirCount.pop_back();
            top--;
        }
    }//while(top>=0)
    ff.Close();
   
    return 0;   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pachleng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值