0

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?

Joe
  • 461
  • 1
  • 3
  • 15

1 Answers1

1

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;

        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • All right, I got the XML, but do I use it in the above method? – Joe Jul 12 '22 at 10:33
  • You need to modify code for your use. Instead of creating the XML create your Laserfiche repository – jdweng Jul 12 '22 at 11:07
  • I upvoted for your effort, but if you remove the XML part how is it different from the attached code, it still does not solve getting the parent folders to create the child. – Joe Jul 12 '22 at 17:00
  • With your code how do you get the parent folder? Using recursion you have access to each subfolder. You method only returns a list of filenames. – jdweng Jul 12 '22 at 17:04
  • That is why you need recursion. Every child folder recursively calls WriteTree(). – jdweng Jul 12 '22 at 17:12
  • Yes, but createfolder needs 2 parameters, maybe because I am a beginner but I am failing to adapt your code. – Joe Jul 12 '22 at 17:19
  • Modify WriteTree(DirectoryInfo info) and add parent to parameter list : WriteTree(DirectoryInfo info, string parent) – jdweng Jul 12 '22 at 17:26
  • Sorry, not working, either files or folders created at same level or not at all. The method I linked did the same thing. – Joe Jul 13 '22 at 05:47