Replicate directory structure in Laserfiche in C#

206 Views Asked by At

I was asked to copy a local 1TB folder worth of data to a Laserfiche repository using a C# tool. Usually to copy a folder tree I use this great method.

I am enumerating the local folder using this method, in order to ONLY get the folder and its tree.

IEnumerable<string> AllFolders2(string root)
{
    var folders = Directory.EnumerateDirectories(root, "*", SearchOption.AllDirectories).Select(path => path.Replace(root, ""));
    var listOfFiles = folders.ToList();
    return listOfFiles; 
}

My problem is the Laserfiche SDK that I am allowed to work with. I the linked method it uses Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath)); but the SDK is giving me a method called

void Create(string Name, LFFolder ParentFolder, bool AutoRename); that requires the parent folder too.

I've created this method to create a folder

void CreateFolder(string FolderName, string ParentPath)
{
     try
     {
        lfParentFolder = (LFFolder)lfDB.GetEntryByPath(ParentPath);
        lfFolder = new LFFolder();

        try
        {
           lfFolder = (LFFolder)lfDB.GetEntryByPath(string.Format(@"{0}\\{1}", lfParentFolder.FullPath, FolderName));
        }
        catch (COMException ex)
        {
           if (ex.ErrorCode == -1073470679)
           {
              lfFolder.Create(FolderName, lfParentFolder, false);
           }
        }
      }
      catch (Exception ex)
      {
          throw ex;
      }

 }

I'm stuck with getting the parent folders for the deeper levels. The SDK is 8.1 Any ideas?

1

There are 1 best solutions below

8
jdweng On

You need to recursively go through each directory instead of using SearchOption.AllDirectories. Here is an example of recursively going through folders. The code crates an XML file of all files in a folder (and child folders).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace SAveDirectoriesXml
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        const string FOLDER = @"c:\temp";
        static XmlWriter writer = null;
        static void Main(string[] args)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            writer = XmlWriter.Create(FILENAME, settings);
            writer.WriteStartDocument(true);

            DirectoryInfo info = new DirectoryInfo(FOLDER);
            WriteTree(info);
            
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
            Console.WriteLine("Enter Return");
            Console.ReadLine();

        }
        static long WriteTree(DirectoryInfo info)
        {
            long size = 0;
            writer.WriteStartElement("Folder");
            try
            {
                writer.WriteAttributeString("name", info.Name);
                writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
                writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
                writer.WriteAttributeString("date", info.LastWriteTime.ToString());
             

                foreach (DirectoryInfo childInfo in info.GetDirectories())
                {
                    size += WriteTree(childInfo);
                }
                
            }
            catch (Exception ex)
            {
                string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
                Console.WriteLine(errorMsg);
                writer.WriteElementString("Error", errorMsg);
            }

            FileInfo[] fileInfo = null;
            try
            {
                fileInfo = info.GetFiles();
            }
            catch (Exception ex)
            {
                string errorMsg = string.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
                Console.WriteLine(errorMsg);
                writer.WriteElementString("Error",errorMsg);
            }

            if (fileInfo != null)
            {
                foreach (FileInfo finfo in fileInfo)
                {
                    try
                    {
                        writer.WriteStartElement("File");
                        writer.WriteAttributeString("name", finfo.Name);
                        writer.WriteAttributeString("size", finfo.Length.ToString());
                        writer.WriteAttributeString("date", info.LastWriteTime.ToString());
                        writer.WriteEndElement();
                        size += finfo.Length;
                    }
                    catch (Exception ex)
                    {
                        string errorMsg = string.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
                        Console.WriteLine(errorMsg);
                        writer.WriteElementString("Error", errorMsg);
                    }
                }
            }

            writer.WriteElementString("size", size.ToString());
            writer.WriteEndElement();
            return size;

        }
    }
}