I know how to find the count of all files inside a folder in Google Drive. But I need to know the count of items that exist in the root of my Google Drive. How can I do that? I can't find an option, and I can't find the answer through search.
Google-drive – How to count all the files inside Google Drive root
google-drive
Related Solutions
I created a command-line tool to handle this.
After following the setup instructions on the GitHub project, you can run the tool with a specified path prefix and the email address of the new owner:
python transfer.py PATH-PREFIX NEW-OWNER-EMAIL
After getting authorization from OAuth, it will recursively walk the Google Drive account it got authorization to, match against the path prefix (ignoring non-matches), and then transfer ownership to the new email. If the new email address indicates a user who isn't even shared on the item yet, it will handle that as well. The current owner will not lose access, but the new or old owner can revoke it.
I should note that it's not always possible to transfer ownership of an item in Drive. For example, it's not possible to move ownership for an item from G Suite (Google Apps for Work) account to a normal GMail account. It can be shared (which the script will do), but ownership transfer will fail.
UPD: as Google started to block unverified scripts, the only way to use it is copy-paste the source to your script editor. And verification requires some extra effort, so I didn't do it yet.
UPD2: if you have a lot of files it will reach the execution time limit and stop; you will need to run it multiple times (you can also set up automatic timed trigger for this)
I wrote a simple script for this. You can run it from here: https://script.google.com/macros/s/AKfycbyUvNoXzBMBDE9pnHkLUltliGwjip5x09t3PeTY_1KoXO45F6iz/exec
(if it gets stopped, just run it once again, and it will continue from where it left)
It will create two files in the root of your Drive, one displays progress and is deleted after script is completed. Other is the report which lists all folders and sizes. It looks like this.
Or you can just copy and paste the code into Google Script editor and run "doGet()" function from there:
function doGet(){
var progressFileCompletedFound = DriveApp.getRootFolder().searchFiles("title contains 'Folder Sizes Report Completed'");
if(progressFileCompletedFound.hasNext()) {
return ContentService.createTextOutput("Report file was already created in your Drive's root folder, exiting.");
}
CreateReportFile();
DriveApp.createFile("Folder Sizes Report Completed.txt", "You may safely delete this file.");
return ContentService.createTextOutput("Report file created in your Drive's root folder");
}
function CreateReportFile() {
var reportContent = CreateReport();
DriveApp.createFile('Folder Sizes Report.txt', reportContent);
}
function CreateReport(){
var reportContent = "";
var progressFileFound = DriveApp.getRootFolder().searchFiles("title contains 'Getting Folder Sizes,'");
var progressFile;
var report=[];
if(progressFileFound.hasNext()) {
progressFile = progressFileFound.next();
var json = progressFile.getBlob().getDataAsString();
try{
report = JSON.parse(json);
} catch(Exception) {
DriveApp.removeFile(progressFile);
progressFile = DriveApp.createFile("Getting Folder Sizes, 0 processed...", " ");
}
}
else {
progressFile = DriveApp.createFile("Getting Folder Sizes, 0 processed...", " ");
}
var f = DriveApp.getRootFolder();
AddFolderToReport(report, f, "/", progressFile);
DriveApp.removeFile(progressFile);
reportContent += "TotalSize MB FilesSize MB Path \r\n";
for(var i=0; i<report.length; i++)
reportContent += Utilities.formatString("%12.2f ", (report[i].totalSize / (1024*1024))) + Utilities.formatString("%11.2f ",(report[i].filesSize / (1024*1024))) + report[i].folderPath + "\r\n";
return reportContent;
}
function AddFolderToReport(report, currentFolder, currentPath, progressFile){
var report1 = [];
for(var i=0; i<report.length; i++)
if(report[i].folderPath == currentPath)
return report[i].totalSize;
var fChildren = currentFolder.getFolders();
var totalSize = 0;
while(fChildren.hasNext() && currentPath.length < 2000){
var nextF = fChildren.next();
totalSize += AddFolderToReport(report, nextF, currentPath + nextF.getName() + "/", progressFile);
}
var filesSize = 0;
var files = currentFolder.getFiles();
while(files.hasNext()){
filesSize += files.next().getSize();
}
totalSize += filesSize;
report.push({folderPath: currentPath, filesSize: filesSize, totalSize: totalSize});
progressFile.setName("Getting Folder Sizes, " + report.length + " processed...");
progressFile.setContent(JSON.stringify(report));
return totalSize;
}
UPD: the script was updated so that if it runs too long and is stopped, just run it once more and it will resume from where it left, using data stored in "Getting Folder Sizes ..." file.
Related Topic
- Google Drive – How to Share a Folder Not in the Root
- Google-drive – How to use Google Drive advanced search to find files that are not shared
- Google Drive – How to Create a Folder Alias
- Google-apps-script – How to know what files on Google Drive (sheets, docs, slides) has GAS inside
- Google Drive – How to View Number of Files in a Folder
- Google-drive – My files on the Google Drive disappeared but are still searchable and use up the quota
Best Answer
Short Answer
Google Drive UI doesn't include a way to count files in My Unit but not in other folders.
Alternatives
Google Apps Script.
From https://developers.google.com/apps-script/reference/drive/drive-app#getFiles()
Google Drive for PC/Mac
Use the Windows / Finder features to count the files for you.
Protip: Deselect all the folders so only files directly in My Unit will be synced. For further details, see Choose what syncs to your computer - Drive Help