AnsweredAssumed Answered

Count all uploaded documents in Share sites

Question asked by ashankar on Apr 24, 2013
Latest reply on May 28, 2013 by rajupingili
Alfresco gurus,

After looking fruitlessly for a tool that provides an accurate total count of uploaded files in the Share document libraries, I've cobbled together the following, inelegant Javascript script (I'm a fortran programmer!).  I've verified the count successfully against test Alfresco instances (versions 3.2r2 and 4.x) by going into each site and manually counting every file uploaded.  Please take a look and see if I've goofed up somewhere.


//
// Script to recursively descend into sites and count files in document libraries
//

var count = 0;

// Get an array of all site nodes
var sites = siteService.listSites(null,null,0);

// Loop over sites
for each (var site in sites) {
     //logger.log("Site: " + site.shortName);
    
     // First, find files at the top level in /documentLibrary (excluding subfolders)

     // Loop over the sites's children (doclib, wiki/blog, etc.)
     for each (child in site.node.children) {

         // Loop over the children of doclib/wiki/blog and get files in the doclib
         for each (item in child.children) {
                // Find unique files
                ufile = unique(item);

                if (ufile.isDocument && ufile.displayPath.match(/documentLibrary/)) {
                        //logger.log("   " + ufile.displayPath + "/" + ufile.name);
                        count++;
                }
          }
     }
    // Now add the file nodes in /documentLibrary subfolders
    getSpaces (site.node, 0);
}
logger.log("Total: " + count);

// Function to find every file in a doclib subfolder in a site
function getSpaces (node, depth) {
// Recursively descend into folders and files all files
  if (node.isContainer) {
       //logger.log("Folder: " + node.displayPath + "/" + node.name);
       for each (var file in node.children) {
         if (file.displayPath.match(/documentLibrary/) && file.isDocument) {
           //logger.log("   " + file.displayPath + "/" + file.name);
           count++;
         }
       }
  }
  var children = node.children;
  for each (var child in children) {
    if (child.isContainer && node.children.size != 0) {
      getSpaces(child, depth++);

    }
  }
}

// Function to find unique members of an array
function unique (origArr) {
    var newArr = [];
        found = undefined;
        x = 0;
        y = 0;
        origLen = origArr.length,
        found,
        x, y;

    for ( x = 0; x < origLen; x++ ) {
        found = undefined;
        for ( y = 0; y < newArr.length; y++ ) {
            if ( origArr[x] === newArr[y] ) {
              found = true;
              break;
            }
        }
        if ( !found) newArr.push( origArr[x] );
    }
   return newArr;
};


Regards,

Anurag

Outcomes