8

I'm using the web version of Google Drive, which is not synced with any computer. I want to view the number of files inside this folder. There are hundreds of files in this folder, so scrolling through them is not an option. How would I be able to do this?

1

Use google colab to find the number of files in a folder

  1. Go to this notebook
  2. File -> Save a copy in Drive
  3. The new copy will open automatically
  4. Run the code blocks by selecting and pressing Shift+Enter
  5. Mount the google drive when you are prompted
  6. Change the value of variable FOLDER_PATH from labelled-dataset to your own folder name or path. For example you can change it to: 'my folder/category/sub folder'
  7. Now run this block with Shift+Enter, this will print the number of files in that folder

OR

Alternatively you can create a new colab notebook and run the following code while following only step 6 from above:

1. Mount Drive:

from google.colab import drive
drive.mount('/content/drive')

2. Count no. of files:

import os
FOLDER_PATH = 'labelled-dataset'
ROOT_PATH = '/content/drive/MyDrive/'
print(len(os.listdir(os.path.join(ROOT_PATH, FOLDER_PATH))))
4

Copied from here but very similar to other versions:

// replace your-folder below with the folder for which you want a listing
function listFolderContents() {
  var foldername = "WA";
  var folderlisting = 'listing of folder ' + foldername;
  var folders = DriveApp.getFoldersByName(foldername)
  var folder = folders.next();
  var contents = folder.getFiles();
  var ss = SpreadsheetApp.create(folderlisting);
  var sheet = ss.getActiveSheet();
  sheet.appendRow( ['name', 'link'] );
  var file;
  var name;
  var link;
  var row;
  while(contents.hasNext()) {
    file = contents.next();
    name = file.getName();
    link = file.getUrl();
    sheet.appendRow( [name, link] );     
  }  
};

I chose folder WA for testing and the results (names and URLs) were returned to a file called list of folder WA (so your-folder in the comment is WA in the code above).

Open a sheet (a new one perhaps), Tools > Script editor and copy all of the above code into the code window. No need to give it or the project a name but you will need to save the code and to authorise it. Whereupon Google will probably object, but not prevent the code working. Then Run it and go find file list of folder xxxx. The count of files may be read from the row number of the last entry (less one for the headings).

Substitute the following for Lines 10 - 19 to return files sizes also:

sheet.appendRow( ['name', 'link','size'] );
  var file;
  var name;
  var link;
  var size;
  var row;
  while(contents.hasNext()) {
    file = contents.next();
    name = file.getName();
    link = file.getUrl();
    size = file.getSize();
    sheet.appendRow( [name, link, size] ); 
2
  • I totally missed that, the file is there. Looks like it works, thanks. Just one thing to note for anyone reading this: it doesn't seem to work with directories, just files (which isn't a problem for me) – Skeleton Bow Dec 5 '17 at 14:15
  • 1
    By the way, how does this work for folders with non-unique names? Would you have to change the name to be unique and then revert it? I tried putting two folders with the same name inside the same parent folder, and it seems to have output the information for the folder nearer to the top. – Skeleton Bow Dec 5 '17 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.