Splitting and Merging PST files

Aspose.Email API provides the capability to split a single PST file into multiple PST files of the desired file size. It can also merge multiple PST files into a single PST file. Both the splitting and merging of PSTs operations can be tracked by adding events to these operations.

Splitting into multiple PSTs

The following code snippet shows you how to split multiple PSTs.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
string dataDir = RunExamples.GetDataDir_Outlook();
try
{
String dstSplit = dataDir + Convert.ToString("Chunks\\");
// Delete the files if already present
foreach (string file__1 in Directory.GetFiles(dstSplit))
{
File.Delete(file__1);
}
using (PersonalStorage personalStorage = PersonalStorage.FromFile(dataDir + "Sub.pst"))
{
// The events subscription is an optional step for the tracking process only.
personalStorage.StorageProcessed += PstSplit_OnStorageProcessed;
personalStorage.ItemMoved += PstSplit_OnItemMoved;
// Splits into pst chunks with the size of 5mb
personalStorage.SplitInto(5000000, dataDir + @"\Chunks\");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + "\nThis example will only work if you apply a valid Aspose Email License. You can purchase full license or get 30 day temporary license from http:// Www.aspose.com/purchase/default.aspx.");
}

Splitting PST based on Specified Criterion

The following code snippet shows you how to split PST based on a specified criterion.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
string dataDir = RunExamples.GetDataDir_Outlook();
IList<MailQuery> criteria = new List<MailQuery>();
PersonalStorageQueryBuilder pstQueryBuilder = new PersonalStorageQueryBuilder();
pstQueryBuilder.SentDate.Since(new DateTime(2005, 04, 01));
pstQueryBuilder.SentDate.Before(new DateTime(2005, 04, 07));
criteria.Add(pstQueryBuilder.GetQuery());
pstQueryBuilder = new PersonalStorageQueryBuilder();
pstQueryBuilder.SentDate.Since(new DateTime(2005, 04, 07));
pstQueryBuilder.SentDate.Before(new DateTime(2005, 04, 13));
criteria.Add(pstQueryBuilder.GetQuery());
if (Directory.GetFiles(dataDir + "pathToPst", "*.pst").Length == 0)
{
}
else
{
string[] files = Directory.GetFiles(dataDir + "pathToPst");
foreach (string file in files)
{
if(file.Contains(".pst"))
File.Delete(file);
}
}
using (PersonalStorage personalStorage = PersonalStorage.FromFile(dataDir + "PersonalStorage_New.pst"))
{
personalStorage.SplitInto(criteria, dataDir + "pathToPst");
}

Merging into a single PST

The following code snippet shows you how to merge into a single PST.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
string dst = dataDir + "Sub.pst";
totalAdded = 0;
try
{
using (PersonalStorage personalStorage = PersonalStorage.FromFile(dst))
{
// The events subscription is an optional step for the tracking process only.
personalStorage.StorageProcessed += PstMerge_OnStorageProcessed;
personalStorage.ItemMoved += PstMerge_OnItemMoved;
// Merges with the pst files that are located in separate folder.
personalStorage.MergeWith(Directory.GetFiles(dataDir + @"MergePST\"));
Console.WriteLine("Total messages added: {0}", totalAdded);
}
Console.WriteLine(Environment.NewLine + "PST merged successfully at " + dst);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + "\nThis example will only work if you apply a valid Aspose Email License. You can purchase full license or get 30 day temporary license from http:// Www.aspose.com/purchase/default.aspx.");
}

Merging Folders from another PST

The following code snippet shows you how to merge folders from another PST.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
string dataDir = RunExamples.GetDataDir_Outlook();
try
{
using (PersonalStorage destinationPst = PersonalStorage.FromFile(dataDir + @"destination.pst"))
using (PersonalStorage sourcePst = PersonalStorage.FromFile(dataDir + @"source.pst"))
{
FolderInfo destinationFolder = destinationPst.RootFolder.AddSubFolder("FolderFromAnotherPst");
FolderInfo sourceFolder = sourcePst.GetPredefinedFolder(StandardIpmFolder.DeletedItems);
// The events subscription is an optional step for the tracking process only.
destinationFolder.ItemMoved += destinationFolder_ItemMoved;
// Merges with the folder from another pst.
destinationFolder.MergeWith(sourceFolder);
Console.WriteLine("Total messages added: {0}", totalAdded);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + "\nThis example will only work if you apply a valid Aspose Email License. You can purchase full license or get 30 day temporary license from http:// Www.aspose.com/purchase/default.aspx.");
}

Helping Methods

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
static void PstMerge_OnStorageProcessed(object sender, StorageProcessedEventArgs e)
{
Console.WriteLine("*** The storage is merging: {0}", e.FileName);
}
static void PstMerge_OnItemMoved(object sender, ItemMovedEventArgs e)
{
if (currentFolder == null)
{
currentFolder = e.DestinationFolder.RetrieveFullPath();
}
string folderPath = e.DestinationFolder.RetrieveFullPath();
if (currentFolder != folderPath)
{
Console.WriteLine(" Added {0} messages to \"{1}\"", messageCount, currentFolder);
messageCount = 0;
currentFolder = folderPath;
}
messageCount++;
totalAdded++;
}
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
static void destinationFolder_ItemMoved(object sender, ItemMovedEventArgs e)
{
totalAdded++;
}