文件夹复制操作(非递归循环遍历文件夹)

/// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="SourcePath">原始路径</param>
        /// <returns></returns>
        public static bool CreateFolder(string SourcePath)
        {
            try
            {
                Directory.CreateDirectory(SourcePath);
                return true;
            }
            catch
            {
                return false;
            }
        }

 /// <summary>
        /// 复制文件夹[循环遍历]
        /// </summary>
        /// <param name="SourcePath">原始路径</param>
        /// <param name="DestinPath">目地的路径</param>
        /// <returns></returns>
        public static bool  CopyFolder(string SourcePath, string DestinPath)
        {
            if (Directory.Exists(SourcePath))
            {
                CreateFolder(DestinPath);//第一次创建跟目录文件夹
                string sourcePath = SourcePath;//[变化的]原始路径
                string destinPath = DestinPath;//[变化的]目地的路径
                Queue<string> source = new Queue<string>();//存原始文件夹路径
                Queue<string> destin = new Queue<string>();//存目地的文件夹路径
                bool IsHasChildFolder = true;//是否有子文件夹
                string tempDestinPath = string.Empty;//临时目地的,将被存于destin中
                while (IsHasChildFolder)
                {
                    string[] fileList = Directory.GetFileSystemEntries(sourcePath);// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
                    for (int i = 0; i < fileList.Length; i++)// 遍历所有的文件和目录
                    {
                        tempDestinPath = destinPath + "\\" + Path.GetFileName(fileList[i]);//取得子文件路径
                        if (Directory.Exists(fileList[i]))//存在文件夹时
                        {
                            source.Enqueue(fileList[i]);//当前的子目录的原始路径进队列
                            destin.Enqueue(tempDestinPath);//当前的子目录的目地的路径进队列
                            CreateFolder(tempDestinPath);//创建子文件夹
                        }
                        else//存在文件
                        {
                            File.Copy(fileList[i], tempDestinPath, true);//复制文件
                        }
                    }
                    if (source.Count > 0 && source.Count == destin.Count)//存在子文件夹时
                    {
                        sourcePath = source.Dequeue();
                        destinPath = destin.Dequeue();
                    }
                    else
                    {
                        IsHasChildFolder = false;
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }
时间: 2024-08-03 23:41:07

文件夹复制操作(非递归循环遍历文件夹)的相关文章

C语言实现二叉树的常用的算法(递归与非递归实现遍历)

队列头文件: #include <stdio.h> #include "BinaryTree.h" // // 队列头文件:Queue.h #ifndef QUEUE_H #define QUEUE_H // // 队列最大元素个数 #define MAX_QUEUE_SIZE 10 typedef BTree QueueElemType; // // 队列结构体 typedef struct tagQueue { BTree *base; int front; // 头指

c++ c 树-不用栈的非递归二叉树遍历 求改正 (C++新手)

问题描述 不用栈的非递归二叉树遍历 求改正 (C++新手) 原程序如下,添加了双亲域parent和标志域mark:不知道哪里不对: #include""iostream""using namespace std;class BinTree; class BinNode{private: int data; BinNode *lchild;BinNode *rchild;BinNode *parent;int mark;friend class BinTree; };

非递归二叉树遍历-c语言中函数指针作为参数与函数的嵌套

问题描述 c语言中函数指针作为参数与函数的嵌套 函数指针作为另一函数的参数和函数的嵌套的区别,感觉都是调用,有什么不一样呢?他们都适用在什么情况下!(我是在学非递归遍历二叉树时看到的) Status Visit(TElemType e){ printf("%cn",e); return OK; } Status InOrderTraverse(BiTree T ,Status(*Visit)(TElemType e)){ SqStack S; InitStack(S); Push(S,

深入遍历二叉树的各种操作详解(非递归遍历)_C 语言

先使用先序的方法建立一棵二叉树,然后分别使用递归与非递归的方法实现前序.中序.后序遍历二叉树,并使用了两种方法来进行层次遍历二叉树,一种方法就是使用STL中的queue,另外一种方法就是定义了一个数组队列,分别使用了front和rear两个数组的下标来表示入队与出队,还有两个操作就是求二叉树的深度.结点数... 复制代码 代码如下: #include<iostream>#include<queue>#include<stack>using namespace std;/

中序遍历二叉树-二叉树的非递归操作。。

问题描述 二叉树的非递归操作.. 如何用栈实现二叉树的非递归操作,越详细越好,谢谢各位啦.一定要详细哦 解决方案 void inOrder2(BinTree *root) //非递归中序遍历 { stack<BinTree*> s; BinTree *p=root; while(p!=NULL||!s.empty()) { while(p!=NULL) { s.push(p); p=p->lchild; } if(!s.empty()) { p=s.top(); cout<<

深入理解二叉树的非递归遍历_C 语言

二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的.对于二叉树,有前序.中序以及后序三种遍历方法.因为树的定义本身就是递归定义,因此采用递归的方法去实现树的三种遍历不仅容易理解而且代码很简洁.而对于树的遍历若采用非递归的方法,就要采用栈去模拟实现.在三种遍历中,前序和中序遍历的非递归算法都很容易实现,非递归后序遍历实现起来相对来说要难一点.一.前序遍历前序遍历按照"根结点-左孩子-右孩子"的顺序进行访问.1.递归实现 复制代码 代码如下: void preO

二叉树递归和非递归遍历

二叉树是一种非常重要的数据结构,很多其他数据机构都是基于二叉树的基础演变过来的.二叉树有前.中.后三种遍历方式,因为树的本身就是用递归定义的,因此采用递归的方法实现三种遍历,不仅代码简洁且容易理解,但其开销也比较大,而若采用非递归方法实现三种遍历,则要用栈来模拟实现(递归也是用栈实现的).下面先简要介绍三种遍历方式的递归实现,再详细介绍三种遍历方式的非递归实现. 一.三种遍历方式的递归实现(比较简单,这里不详细讲解) 1.先序遍历--按照"根节点-左孩子-右孩子"的顺序进行访问. vo

Java遍历文件夹的2种方法

A.不使用递归的方法: import java.io.File; import java.util.LinkedList; public class FileSystem { public static void main(String[] args) { long a = System.currentTimeMillis(); LinkedList list = new LinkedList(); File dir = new File("c:\\Program Files\\Java\\&q

二叉搜索树与树的遍历非递归练习

复习了二叉搜索树的实现,包括插入.查找和删除操作,顺便做了下二叉树的三种遍历操作.全部操作采用非递归方式.   #include<iostream>   #include<stack>   using namespace std;     typedef int T;   // 值类型      // 节点定义    struct node {       T val;       node *left,*right;       node(const T& val):va