4

for v2, we can get an example how to rename file with google drive API. here's the link https://developers.google.com/drive/api/v2/reference/files/patch#examples
here's how we can rename a file with v2 in javascript

/**
 * Rename a file.
 *
 * @param {String} fileId <span style="font-size: 13px; ">ID of the file to rename.</span><br> * @param {String} newTitle New title for the file.
 */
function renameFile(fileId, newTitle) {
  var body = {'title': newTitle};
  var request = gapi.client.drive.files.patch({
    'fileId': fileId,
    'resource': body
  });
  request.execute(function(resp) {
    console.log('New Title: ' + resp.title);
  });
}

i need to create function like example from v2 with electron and nodejs. here's what i've done so far mfm-gdrive

dhanyn10
  • 704
  • 1
  • 9
  • 26
  • Can you post the code you are using in the question and what exactly is not working? @dhanyn10 – ale13 Nov 09 '20 at 15:36
  • i can't find any example for function renaming file in v3. @ale13 – dhanyn10 Nov 09 '20 at 16:22
  • Now I noticed that you wanted the script for Node.js. From your question, I had thought that you wanted the script for Javascript. This is my poor English skill. I deeply apologize for this. I understood my answer was not suitable for your question. So I have to delete it because I don't want to confuse other users. I deeply apologize for my poor English skill again. – Tanaike Nov 17 '20 at 08:08

1 Answers1

7

If you want to rename a file using the Drive API v3, you will have to use the Files:update request, like this:

function renameFile(auth) {
  const drive = google.drive({version: 'v3', auth});
  var body = {'name': 'NEW_NAME'};
  drive.files.update({
    fileId: 'ID_OF_THE_FILE',
    resource: body,
  }, (err, res) => {
    if (err) return console.log('The API returned an error: ' + err);
    else {
      console.log('The name of the file has been updated!');
    }
  });
}

You can also simulate the update request by using the Drive API v3 Reference here.

As for examples, I suggest you check the Drive API v3 Node.js Quickstart here which you can later adapt such that is suits your needs accordingly.

Reference

ale13
  • 5,679
  • 3
  • 10
  • 25