Pst Folder getContentCount

Hi team,
can i get Path with folder with mail count in brackets in pst,
for ex: Top of personal folder/inbox[0]/test[1]/… so on

@pradeepnegi,

Thank you for contacting Aspose Support.
Please use the code snippet given below to get the mail count of all the folders in PST.

PersonalStorage pst = PersonalStorage.FromFile(filePath, true);
FolderInfo rootFolder = pst.RootFolder;
DisplayFolderContents(rootFolder);

private static void DisplayFolderContents(FolderInfo folderInfo)
{
    if (folderInfo.HasSubFolders == true)
    {
        foreach (FolderInfo subfolderInfo in folderInfo.GetSubFolders())
        {
            Console.WriteLine(subfolderInfo.DisplayName + "[" + subfolderInfo.ContentCount + "]");
            DisplayFolderContents(subfolderInfo);
        }
    }
}

We hope that this answered your question. Please feel free to reach us if additional information is required.

No i dont wanna print this way i want full path with folderContentCount. i want to create tree from this path in java

@pradeepnegi,

You may generate similar output to the one shared in your initial message by using the code snippet given below.

PersonalStorage pst = PersonalStorage.fromFile(filePath, true);
FolderInfo rootFolder = pst.getRootFolder();
String path = rootFolder.getDisplayName();
path = path + displayFolderContents(rootFolder);
System.out.println(path);

private static String displayFolderContents(FolderInfo folderInfo) {
    String path = "";
    if (folderInfo.hasSubFolders()){
        for (FolderInfo subfolderInfo : folderInfo.getSubFolders()){
            path = path + "/" + subfolderInfo.getDisplayName() + "[" + subfolderInfo.getContentCount() + "]";
            path = path + displayFolderContents(subfolderInfo);
        }
    }
    return path;
}

You may modify the code provided according to your specific requirements.

can i customized this method “FolderInfo.retrieveFullPath()” , where each folder contains it mail count with full path.
like this - Top of personal folder[0];
Top of Personal folder[0]\Inbox[1];
Top of Personal folder[0]\Inbox[1]\test[10];

@pradeepnegi,

Thank you for your feedback.
Please use the code snippet given below to generate the folder paths with the content count.

private static void getAllFolderPaths() {
    PersonalStorage pst = PersonalStorage.fromFile(fileName, true);
    FolderInfo rootFolder = pst.getRootFolder();

    HashMap<String, Integer> contents = new HashMap<>();
    contents.put(rootFolder.getDisplayName(), rootFolder.getContentCount());

    HashMap<String, String> paths = new HashMap<>();
    paths.put(rootFolder.getDisplayName(), rootFolder.getDisplayName() + "[" + rootFolder.getContentCount() + "]");

    getFolderContentCount(rootFolder, contents);
    getFolderPaths(rootFolder, paths, contents);

    for(String name: paths.keySet()){
        String path = paths.get(name);
        System.out.println(name + " -> " + path);
    }
}

private static void getFolderContentCount(FolderInfo folderInfo, HashMap<String, Integer> contents) {
    if (folderInfo.hasSubFolders()) {
        for (FolderInfo subfolderInfo : folderInfo.getSubFolders()){
            contents.put(subfolderInfo.getDisplayName(), subfolderInfo.getContentCount());
            getFolderContentCount(subfolderInfo, contents);
        }
    }
}

private static void getFolderPaths(FolderInfo folderInfo, HashMap<String, String> paths, HashMap<String, Integer> contents){
    if (folderInfo.hasSubFolders()) {
        for (FolderInfo subfolderInfo : folderInfo.getSubFolders()){
            paths.put(subfolderInfo.getDisplayName(), generateFolderPath(subfolderInfo.retrieveFullPath(), contents));
            getFolderPaths(subfolderInfo, paths, contents);
        }
    }
}

private static String generateFolderPath(String fullPath, Map contents) {
    String path = "";
    String[] parts = fullPath.split(Matcher.quoteReplacement(System.getProperty("file.separator")));
    for(String part : parts){
        path = path + part + "[" + contents.get(part) + "]" + System.getProperty("file.separator");
    }
    return path.substring(0, path.length() - 1);
}

We hope that this answered your question. Please feel free to reach us if additional information is required.

This code doesn’t give path in sequence like folderInfo.retrieveFullPath. how do i get path in sequence.

@pradeepnegi,

Please elaborate further regarding the issue that you are facing. In our testing, folderInfo.retrieveFullPath returned a path like “Top of Personal Folders\Inbox\Test1\Test2”. The code given in the previous message changes this path to “Top of Personal Folders[0]\Inbox[1]\Test1[0]\Test2[2]”. The path remains the same.

In this code there is a problem, when i am creating folder in pst with same name its content count is not updating. may be this problem is arises due to hashmap, because hashmap doesn’t contain duplicate values in it

@pradeepnegi,

Thank you for your feedback.

You may modify the code as shown below to handle folders with the same name.

private static void getFolderContentCount(FolderInfo folderInfo, HashMap<String, Integer> contents) {
    if (folderInfo.hasSubFolders()) {
        for (FolderInfo subfolderInfo : folderInfo.getSubFolders()){
            contents.put(subfolderInfo.retrieveFullPath(), subfolderInfo.getContentCount());
            getFolderContentCount(subfolderInfo, contents);
        }
    }
}

private static void getFolderPaths(FolderInfo folderInfo, HashMap<String, String> paths, HashMap<String, Integer> contents){
    if (folderInfo.hasSubFolders()) {
        for (FolderInfo subfolderInfo : folderInfo.getSubFolders()){
            paths.put(subfolderInfo.retrieveFullPath(), generateFolderPath(subfolderInfo.retrieveFullPath(), contents));
            getFolderPaths(subfolderInfo, paths, contents);
        }
    }
}

private static String generateFolderPath(String fullPath, HashMap<String, Integer> contents) {
    String path = "";
    String[] parts = fullPath.split(Matcher.quoteReplacement(System.getProperty("file.separator")));
    int i = 0;
    for(String part : parts){
        int j = 0;
        String identifier = "";
        while(j <= i){
            identifier = identifier + parts[j] + "\\";
            j++;
        }
        identifier = identifier.substring(0, identifier.length() - 1);
        path = path + part + "[" + contents.get(identifier) + "]" + System.getProperty("file.separator");
        i++;
    }
    return path.substring(0, path.length() - 1);
}

We hope that the code given above solved the issue that you were facing. Please feel free to reach us if additional information is required.