10

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?

3 Answers 3

2

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))))
1
  • If there are subfolders and you want to count files in those folders also, then modify the last line as follows: print(sum([len(files) for r, d, files in os.walk(os.path.join(ROOT_PATH, FOLDER_PATH))]))
    – Imran
    Commented Oct 23, 2023 at 9:10
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) Commented Dec 5, 2017 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. Commented Dec 5, 2017 at 14:17
0

This is a slightly more thorough version of @penduDev's answer (so credit goes to them). I much prefer this version because it doesn't involve copying someone else's notebook (it shows you from scratch how to make your own).

  1. Go to Google Colab
  2. Click 'New Notebook'
  3. Locate the box that allows you to enter python code (see image):

enter image description here

  1. Paste this code into the little box
from google.colab import drive
drive.mount('/content/drive')
  1. Run it by pressing the little 'play' button to the left of the code you just entered.

  2. Since you're asking this script to look into your google drive, you'll be prompted to allow it access to your google drive

  3. Press '+ Code' to create another code box

  4. Inside it put this

import os
FOLDER_PATH = '/content/drive/MyDrive'
print(len(os.listdir(os.path.join(FOLDER_PATH))))
  1. Hit the little play button to run it. This code will count the number of items in the top level of your google drive. Now we know it works.

  2. To count the items in a specific folder within your Google Drive, add the folder path to the end of this bit '/content/drive/MyDrive'.

Here's an example:

import os
FOLDER_PATH = '/content/drive/MyDrive/pics2020/US and Mexico'
print(len(os.listdir(os.path.join(FOLDER_PATH))))

This will go into a folder called 'pics2020', then into the folder inside called 'US and Mexico', and it will count the items in there.

Notes

  • If step 8 worked but step 10 didn't, you most likely have the wrong path to your directory. AFAIK the Google Drive web interface won't let you copy/paste the directory path (which is annoying), so just be on the lookout for typos or you'll get an error.

  • If you get this annoying error

A Google Drive timeout has occurred (most recently at 12:46:20 PM). More info.

The unsatisfying thing to do is just try again as it's a bug between Google Colab and Google Drive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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