0

List Files

by
Published today

Search files and folders in Google Drive with a query, optionally within one folder.

Script gdrive Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 47 minutes ago
1
//native
2

3
export type DynSelect_folder_id = string
4

5
// Dropdown of the account's folders (first 1000, by name).
6
export async function folder_id(auth: RT.Gdrive) {
7
  const url = new URL("https://www.googleapis.com/drive/v3/files")
8
  url.searchParams.append(
9
    "q",
10
    "mimeType = 'application/vnd.google-apps.folder' and trashed = false"
11
  )
12
  url.searchParams.append("fields", "files(id, name)")
13
  url.searchParams.append("orderBy", "name")
14
  url.searchParams.append("pageSize", "1000")
15
  url.searchParams.append("supportsAllDrives", "true")
16
  url.searchParams.append("includeItemsFromAllDrives", "true")
17
  const response = await fetch(url, {
18
    headers: {
19
      Authorization: `Bearer ${auth.token}`,
20
      Accept: "application/json",
21
    },
22
  })
23
  if (!response.ok) {
24
    throw new Error(`${response.status} ${await response.text()}`)
25
  }
26
  const { files } = (await response.json()) as {
27
    files: { id: string; name: string }[]
28
  }
29
  return [
30
    { value: "root", label: "My Drive (root)" },
31
    ...files.map((f) => ({ value: f.id, label: `${f.name} (${f.id})` })),
32
  ]
33
}
34

35
/**
36
 * List Files
37
 * Search files and folders with a Drive query (e.g. `name contains 'report' and mimeType = 'application/pdf'`), optionally restricted to one folder. Includes shared drives; trashed files are excluded unless requested. Returns `files` and `nextPageToken`.
38
 */
39
export async function main(
40
  auth: RT.Gdrive,
41
  q: string | undefined,
42
  folder_id: DynSelect_folder_id | undefined,
43
  include_trashed: boolean = false,
44
  order_by: string | undefined,
45
  page_size: number = 100,
46
  page_token: string | undefined,
47
  fields: string = "nextPageToken, files(id, name, mimeType, parents, size, createdTime, modifiedTime, webViewLink)"
48
) {
49
  const clauses: string[] = []
50
  if (q !== undefined && q !== "") clauses.push(`(${q})`)
51
  if (folder_id !== undefined && folder_id !== "")
52
    clauses.push(`'${folder_id}' in parents`)
53
  if (!include_trashed) clauses.push("trashed = false")
54

55
  const url = new URL("https://www.googleapis.com/drive/v3/files")
56
  if (clauses.length > 0) url.searchParams.append("q", clauses.join(" and "))
57
  url.searchParams.append("pageSize", String(page_size))
58
  url.searchParams.append("fields", fields)
59
  url.searchParams.append("supportsAllDrives", "true")
60
  url.searchParams.append("includeItemsFromAllDrives", "true")
61
  if (order_by !== undefined && order_by !== "")
62
    url.searchParams.append("orderBy", order_by)
63
  if (page_token !== undefined && page_token !== "")
64
    url.searchParams.append("pageToken", page_token)
65

66
  const response = await fetch(url, {
67
    headers: {
68
      Authorization: `Bearer ${auth.token}`,
69
      Accept: "application/json",
70
    },
71
  })
72

73
  if (!response.ok) {
74
    throw new Error(`${response.status} ${await response.text()}`)
75
  }
76

77
  return await response.json()
78
}
79