productiveGuser.com

Get Google Drive File ID

Posted By: Harish
get google drive file ID from the link and the Apps script

When a file or folder is uploaded or created in Google Drive, an ID is assigned to it. This ID can be used to get the details of the file and to do other actions using Apps Script.

In this post, we will discuss different ways to get the File ID using its link or through a script.

Get File ID from its URL

This is the simplest way to know the file ID.

Let's take an example document file URL.

https://docs.google.com/document/d/1-QdZbhH888i58fEoeMRQ_6TnbeHl6Ag6/edit

The file's ID is the unique text between d/ and the next /. So, from our example link, the file ID will be 1-QdZbhH888i58fEoeMRQ_6TnbeHl6Ag6.

Similarly, we can easily identify the File ID's of any type of google drive file.

Get File ID through its name

What if you know the name of the file and want its file ID?

For this we have to visit our drive, search for the file and copy its link to get the file ID.

But with Google Apps script, we can find its ID by running a simple function.

Open Google Apps Script editor and create a new project.

Replace the default function (which already exists) with the below function and run it. Visit creating and executing a Google Apps Script post for more info.

function getGoogleDriveFileId() {
  try {
    const fileName = "productiveGuser Docx File" //<-- change file name here
    const list = DriveApp.getFilesByName(fileName);
    while (list.hasNext()) {
      console.log(`${fileName} ID:`, list.next().getId());
    }
  } catch (err) {
    Logger.log(err)
  }
}

We are using our previously created Microsoft Word document's name for this example.

By running the above function, the document ID will be printed out in the execution log window.

Share is Caring

Related Posts