0

I have this google script which enables us to list of files within google our drive, including the name, the location and it's url. I have been looking quite a while to find a python equivalent to do the same task but no avail. Is there any way, we could convert this gs into a python script?

Thank you very much `

// TODO: Set folder ID
var folderId = '*your folder id is here*';
var array=[];
// Main function 2: List all files & folders, & write into the current sheet.
function listAll(){
  getFolderTree(folderId, true);   
};

// Get Folder Tree
function getFolderTree(folderId, listAll) {
  try {
    // Get folder by id
    var parentFolder = DriveApp.getFolderById(folderId);
    //go to 1st sheet
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
    // Initialise the sheet
    var file, data, sheet = SpreadsheetApp.getActiveSheet();
    sheet.clear();
    // Get files and folders
    getChildFolders(parentFolder.getName(), parentFolder, data, sheet, listAll);
  } 
  catch (e) {
    Logger.log(e.toString());
  }
};

// Get the list of files and folders and their metadata in recursive mode
function getChildFolders(parentName, parent, data, sheet, listAll) {
  var childFolders = parent.getFolders();
  // List folders inside the folder
    while (childFolders.hasNext()) {
    var childFolder = childFolders.next();
    
    // List files inside the folder
    var files = childFolder.getFiles();
    while (listAll & files.hasNext()) {
      var childFile = files.next();
      //Logger.log("File Name: " + childFile.getName());
      data = [ 
        parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
        childFile.getName(),
        childFile.getUrl(),
      ];
      // Write
      array.push(data);
    }
    // Recursive call of the subfolder
    //Logger.log(array);
    getChildFolders(parentName + "/" + childFolder.getName(), childFolder, data, sheet, listAll);  
        SpreadsheetApp.getActiveSheet().getRange(1, 1, array.length, 3).setValues(array);
  };
 
};

`

I have tried this. but it is only getting the title and id, it is still failing on getting the url. I hope you guys could help. Thank you `

# Import PyDrive and associated libraries.
# This only needs to be done once per notebook.
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client.
# This only needs to be done once per notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
import pandas as pd
df=pd.DataFrame(columns=('title', 'id','createdDate','modifiedDate','downloadUrl'))
# List .txt files in the root.
#
# Search query reference:
# https://developers.google.com/drive/v2/web/search-parameters
listed = drive.ListFile({'q': "'19r2AtADzB_5DN0_DFIzhO27LrbYf2WAk' in parents and trashed=false"}).GetList()
file.FetchMetadata()
for file in listed:
 
  listoffile=pd.DataFrame([[file['title'],file['id'],file['createdDate'],file['modifiedDate'],'https://docs.google.com/uc?export=download&id='+file['id']]],columns=('title', 'id','createdDate','modifiedDate','downloadUrl'))
  df=df.append(listoffile)
  

`

Tedinoz
  • 5,911
  • 3
  • 25
  • 35
  • In your situation, is this library useful? https://github.com/tanaikech/getfilelistpy – Tanaike Dec 04 '22 at 22:51
  • Almost working. CMIIW. the problem with getfilelistpy is a tree. what is requested result here is a table or dataframe. Hence, I still strugle to figure it out. – bedy kharisma Dec 05 '22 at 02:19
  • def ListFolder(parent): filelist=[] file_list = drive.ListFile({'q': "'%s' in parents and trashed=false" % parent}).GetList() for f in file_list: if f['mimeType']=='application/vnd.google-apps.folder': # if folder filelist.append([[f['id'],f['title'],ListFolder(f['id'])]]) else: filelist.append([[f['id'],f['title'],f['alternateLink']]]) return filelist ListFolder('root') – bedy kharisma Dec 05 '22 at 02:22

0 Answers0