Skip folder

private bool ConvertOstToPst(string ostFilePath, string pstFilePath)
    {
        try
        {
            using (PersonalStorage ost = PersonalStorage.FromFile(ostFilePath, false))
            {

                if (ost.RootFolder == null)
                {
                    
                    return false;
                }
                nTotalCount = GetTotalItemCount(ost.RootFolder);    
                
                using (PersonalStorage pst = PersonalStorage.Create(pstFilePath, FileFormatVersion.Unicode))
                {
                   
                    var root = pst.RootFolder;
                   
                    FolderInfo inboxFolder = pst.CreatePredefinedFolder("Inbox", StandardIpmFolder.Inbox);
                    FolderInfo sent = pst.CreatePredefinedFolder("Sent Items", StandardIpmFolder.SentItems);
                    FolderInfo drafts = pst.CreatePredefinedFolder("Drafts", StandardIpmFolder.Drafts);
                    FolderInfo junk = pst.CreatePredefinedFolder("Junk Email", StandardIpmFolder.JunkEmail);
                    FolderInfo calendar = root.AddSubFolder("Calendar", "IPF.Appointment");
                    FolderInfo contacts = root.AddSubFolder("Contacts", "IPF.Contact");
                    FolderInfo journal = root.AddSubFolder("Journal", "IPF.Journal");
                    FolderInfo notes = pst.CreatePredefinedFolder("Notes", StandardIpmFolder.Notes);
                    FolderInfo tasks = root.AddSubFolder("Tasks", "IPF.Task");
                    //foreach (FolderInfo subFolder in ost.RootFolder.GetSubFolders())
                    //{

                    //    ProcessFolder(subFolder, pst, ost);
                    //}

                    foreach (var root in ost.RootFolder.GetSubFolders())
                    {
                        try
                        {
                            var ipm = root.GetSubFolder("IPM_SUBTREE");
                            if (ipm != null)
                            {
                                foreach (var sub in ipm.GetSubFolders())
                                {
                                    ProcessFolder(sub, pst, ost);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            LogMessage($"Failed accessing IPM_SUBTREE: {ex.Message}");
                        }
                    }
                    //foreach (FolderInfo subFolder in ost.RootFolder.GetSubFolders())
                    //{
                    //    if (subFolder.DisplayName.Equals("Root - Mailbox", StringComparison.OrdinalIgnoreCase) ||
                    //        subFolder.DisplayName.Equals("Root - Public", StringComparison.OrdinalIgnoreCase))
                    //    {
                    //        try
                    //        {
                    //            var ipm = subFolder.GetSubFolder("IPM_SUBTREE");
                    //            if (ipm != null)
                    //            {
                    //                foreach (var sub in ipm.GetSubFolders())
                    //                {
                    //                    ProcessFolder(sub, pst, ost);

                    //                }
                    //            }
                    //        }
                    //        catch (Exception ex)
                    //        {
                    //            LogMessage($"Error accessing IPM_SUBTREE in {subFolder.DisplayName}: {ex.Message}");
                    //        }
                    //    }
                    //}


                    return true;
                }             
            }
        }
        catch (Exception ex)
        {
            LogMessage($" Conversion failed: {ex.Message}");
            return false;
        }
    }
  • List item

@Anjali12

Cause

It seems you want to skip certain folders while converting OST to PST. The current implementation processes all subfolders under the root folder, which might not be necessary for your use case.

Solution

To skip specific folders during the conversion process, you can add a condition to check the folder’s name or any other property before processing it. This way, you can filter out the folders you don’t want to include in the conversion.

Code

Here’s a modified version of your code snippet that includes a check to skip folders based on their names:

foreach (var root in ost.RootFolder.GetSubFolders())
{
    try
    {
        // Skip folders named "FolderToSkip"
        if (root.DisplayName.Equals("FolderToSkip", StringComparison.OrdinalIgnoreCase))
        {
            continue; // Skip this folder
        }

        var ipm = root.GetSubFolder("IPM_SUBTREE");
        if (ipm != null)
        {
            foreach (var sub in ipm.GetSubFolders())
            {
                ProcessFolder(sub, pst, ost);
            }
        }
    }
    catch (Exception ex)
    {
        LogMessage($"Failed accessing IPM_SUBTREE: {ex.Message}");
    }
}

In this example, replace "FolderToSkip" with the actual name of the folder you wish to skip. You can also extend this logic to skip multiple folders by using a list or array of names.

Next Steps

  1. Test the modified code to ensure it correctly skips the specified folders.
  2. If you need to skip folders based on different criteria (like folder type or other properties), adjust the condition accordingly.
  3. Review the rest of your conversion logic to ensure it aligns with your requirements.

Please implement these changes and let me know if you need further assistance!

Hello @Anjali12,

Thanks for sharing your code.

Can you please clarify what you mean by “Skip folder”?
Are some folders missing in the output PST?

If so, please let us know:

  • Which folder is not copied?
  • Are this folder presents in the original OST?

Please also share a sample OST, that would help reproduce the issue.