Products

Solutions

Learn More

Partners

Community

Blog

About

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesFolderManager Copy FolderFolderManager Copy Folder
Previous
 
Next
New Post
6/19/2018 4:38 PM
 

I was in need of a CopyFolder function for the FolderManager --- FolderManager.Instance.CopyFolder().

Not having found one, I wrote one. If this is useful, please feel free to use it. If you have any suggestions or modifications, let me know.

//Usage

     var sourceFolder = FolderManager.Instance.GetFolder(PortalId, "Assets/_default/");
     var rootDestFolder = FolderManager.Instance.GetFolder(PortalId, "Assets/");
     
     //Copy a folder to a different root folder with a new folder name
     FolderManager.Instance.CopyFolder(sourceFolder, rootDestFolder,"myNewFolderName");
     
     //copy a folder to the same root folder with a new foldername
     FolderManager.Instance.CopyFolder(sourceFolder, sourceFolder, "myNewFolderName");
     
     //copy a folder to a new destination within the same portal
     FolderManager.Instance.CopyFolder(PortalId, sourceFolder, "Assets/myNewFolderName/");

 

//Implementation

using System;
using System.IO;
using System.Linq;
using System.Web;
using DotNetNuke.Services.FileSystem;

namespace DNNExtensions
{
    public static class Extensions
    {
        /// <summary>
        /// Copy an existing folder and contents to the a new folder in the destination. After copying, the destination will
        /// contain a folder with the same name as the source folder.
        /// </summary>
        /// <param name="ifm"></param>
        /// <param name="source">An exising folder</param>
        /// <param name="rootDest">The parent folder into which the new folder will be created</param>
        public static void CopyFolder(this IFolderManager ifm, IFolderInfo source, IFolderInfo rootDest)
        {
            if (!ifm.FolderExists(rootDest.PortalID, rootDest.FolderPath+$"{source.FolderName}/"))
            {
                rootDest=  ifm.AddFolder(rootDest.PortalID, rootDest.FolderPath+$"{source.FolderName}/");
            }
            var diSource = source.PhysicalPath;
            var diDest = rootDest.PhysicalPath;
            _copyFolder(diSource,diDest,rootDest.PortalID);
            ifm.Synchronize(rootDest.PortalID,rootDest.FolderPath);
        }

        /// <summary>
        /// Copy an existing folder and contents to the a new folder in the destination. After copying, the destination will
        /// contain a folder with the specifed newFolderName.
        /// </summary>
        /// <param name="ifm"></param>
        /// <param name="source">An exising folder</param>
        /// <param name="rootDest">The parent folder info into which the new folder will be created</param>
        /// <param name="newFolderName">The name of the new folder</param>
        public static IFolderInfo CopyFolder(this IFolderManager ifm, IFolderInfo source, IFolderInfo rootDest, string newFolderName)
        {
            var newDest=!ifm.FolderExists(rootDest.PortalID, rootDest.FolderPath+newFolderName+"/")?ifm.AddFolder(rootDest.PortalID, rootDest.FolderPath+newFolderName+"/"):ifm.GetFolder(rootDest.PortalID, rootDest.FolderPath+newFolderName+"/");

            var diSource = source.PhysicalPath;
            var diDest = newDest.PhysicalPath;
            _copyFolder(diSource,diDest,newDest.PortalID);
            ifm.Synchronize(rootDest.PortalID,rootDest.FolderPath,true,true);
            return newDest;

        }
        /// <summary>
        /// Copy an existing folder and contents to a new folder using the destination path.
        /// </summary>
        /// <param name="ifm"></param>
        /// <param name="portalId">The portal for the new folder</param>
        /// <param name="source">An exising folder</param>
        /// <param name="destPath">The path and name of the new folder within the portal</param>
        public static IFolderInfo CopyFolder(this IFolderManager ifm, int portalId, IFolderInfo source, string destPath)
        {
            var newDest=!ifm.FolderExists(portalId, destPath)?ifm.AddFolder(portalId, destPath):ifm.GetFolder(portalId,destPath);
            var diSource=source.PhysicalPath;
            _copyFolder(diSource,newDest.PhysicalPath,portalId);
            ifm.Synchronize(portalId,newDest.FolderPath,true,true);
            return newDest;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="ifm"></param>
        /// <param name="source"></param>
        /// <param name="dest"></param>
        /// <param name="recurse">Copy subfolders and files, too</param>
        /// <returns></returns>
        public static IFolderInfo CopyFiles(this IFileManager ifm, IFolderInfo source, IFolderInfo dest, Boolean recurse=false)
        {
            var diSource = new DirectoryInfo(source.PhysicalPath);
            var diDest = new DirectoryInfo(dest.PhysicalPath);
            _copyAll(diSource, diDest, recurse);
            return dest;
        }

        private static void _copyFolder(string sourceDirectory, string targetDirectory, int portalId)
        {
            DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
            DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);

            _copyAll(diSource, diTarget);
           
        }

        private static void _copyAll(DirectoryInfo source, DirectoryInfo target, Boolean recurse=true)
        {
            var dirList = source.GetDirectories().Where(p=>[email protected]"\"!=target.FullName&&p.FullName!=target.FullName);
            // Copy each file into the new directory.
            foreach (System.IO.FileInfo fi in source.GetFiles())
            {
                fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
            }

            // Copy each subdirectory using recursion.
            if (!recurse) return;
            foreach (DirectoryInfo diSourceSubDir in dirList)
            {
                DirectoryInfo nextTargetSubDir =
                    target.CreateSubdirectory(diSourceSubDir.Name);
                _copyAll(diSourceSubDir, nextTargetSubDir);
            }
        }
    }
}

 

 
New Post
6/20/2018 7:33 AM
 
Chris,

propably a good idea, thanks for that - but why don't you go to GitHub, fork the DNN Platform, put hat code into the asset manager and start a pull request? In the forum, no one will find it in a couple of years...

Happy DNNing!
Michael

Michael Tobisch
DNN★MVP
dnn-Connect.org - The most vibrant community around the DNN-platform
 
New Post
6/20/2018 3:45 PM
 

In space, no one can hear you scream? 

I'll take a look at it. I've never posted anything there.

Thanks!

 
New Post
6/20/2018 9:57 PM
 
I generally agree with Michael,

however, please open an issue in the GitHub repository for DNN Platform as well, describing your need and solution.

You will also need to sign a CLA (contributor license agreement), if your suggestion got accepted to get your pull request accepted (sorry, legal necessity)

Cheers from Germany,
Sebastian Leupold

dnnWerk - The DotNetNuke Experts   German Spoken DotNetNuke User Group

Speed up your DNN Websites with TurboDNN
 
Previous
 
Next
HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesFolderManager Copy FolderFolderManager Copy Folder


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.

Content Layout

Subscribe to DNN Digest

Subscribe to DNN Digest

DNN Digest is our monthly email newsletter. It highlights news and content from around the DNN ecosystem, such as new modules and themes, messages from leadership, blog posts and notable tweets. Keep your finger on the pulse of the ecosystem by subscribing.  

What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out