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;   
}

你可能感兴趣的:(windows,算法,vector,null,iostream)