0

Move or Rename File or Folder

by
Published today

Move a file or folder to another folder of the same drive, rename it, or both. `item` and `destination_folder` are each an id, or a path from the drive root starting with "/" (e.g. `/Reports/2026/summary.xlsx`). To move across drives, copy then delete. Requires Files.ReadWrite (a connection default); a drive that is not your own needs Files.ReadWrite.All.

Script ms_onedrive Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 hours ago
1
//native
2

3
const graph = "https://graph.microsoft.com/v1.0"
4

5
function driveUrl(drive_id: string | undefined) {
6
  return drive_id != null && drive_id !== ""
7
    ? `${graph}/drives/${encodeURIComponent(drive_id)}`
8
    : `${graph}/me/drive`
9
}
10

11
// An item is addressed by its id, or by a path from the drive root starting with "/"
12
function ref(item: string | undefined) {
13
  if (item == null || item === "" || item === "/") return "root"
14
  if (!item.startsWith("/")) return `items/${encodeURIComponent(item)}`
15
  const path = item
16
    .replace(/\/+$/, "")
17
    .split("/")
18
    .map(encodeURIComponent)
19
    .join("/")
20
  return `root:${path}:`
21
}
22

23
async function folderId(
24
  token: string,
25
  drive: string,
26
  folder: string
27
): Promise<string> {
28
  if (!folder.startsWith("/")) return folder
29
  const response = await fetch(`${drive}/${ref(folder)}?$select=id`, {
30
    headers: { Authorization: `Bearer ${token}`, Accept: "application/json" },
31
  })
32
  if (!response.ok) {
33
    throw new Error(`${response.status} ${await response.text()}`)
34
  }
35
  return ((await response.json()) as { id: string }).id
36
}
37

38
/**
39
 * Move or Rename File or Folder
40
 * Move a file or folder to another folder of the same drive, rename it, or both. `item` and `destination_folder` are each an id, or a path from the drive root starting with "/" (e.g. `/Reports/2026/summary.xlsx`). To move across drives, copy then delete. Requires Files.ReadWrite (a connection default); a drive that is not your own needs Files.ReadWrite.All.
41
 */
42
export async function main(
43
  auth: RT.MsOnedrive,
44

45
  item: string,
46
  destination_folder: string | undefined,
47
  new_name: string | undefined,
48
  drive_id: string | undefined
49
) {
50
  const drive = driveUrl(drive_id)
51
  const body: { [key: string]: any } = {}
52
  if (new_name != null && new_name !== "") body.name = new_name
53
  if (destination_folder != null && destination_folder !== "") {
54
    body.parentReference = {
55
      id: await folderId(auth.token, drive, destination_folder),
56
    }
57
  }
58
  if (Object.keys(body).length === 0) {
59
    throw new Error("Provide destination_folder, new_name or both")
60
  }
61

62
  const response = await fetch(`${drive}/${ref(item)}`, {
63
    method: "PATCH",
64
    headers: {
65
      Authorization: `Bearer ${auth.token}`,
66
      "Content-Type": "application/json",
67
      Accept: "application/json",
68
    },
69
    body: JSON.stringify(body),
70
  })
71

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

76
  return await response.json()
77
}
78