These are 3 sample scripts for moving a file to the specific folder in Google Drive using Google Apps Script.
Sample script 1
In this script, only Drive Service is used.
var sourceFileId = "###";
var destinationFolderId = "###";
var file = DriveApp.getFileById(sourceFileId);
DriveApp.getFolderById(destinationFolderId).addFile(file);
file
.getParents()
.next()
.removeFile(file);
Sample script 2
In this script, only Drive API at Advanced Google services. (In this case, it’s Drive API v2.)
var sourceFileId = "###";
var destinationFolderId = "###";
Drive.Files.update({ parents: [{ id: destinationFolderId }] }, sourceFileId);
Sample script 3
In this script, only Drive API v3 is used.
var sourceFileId = "###";
var destinationFolderId = "###";
var url1 =
"https://www.googleapis.com/drive/v3/files/" +
sourceFileId +
"?fields=parents";
var res = UrlFetchApp.fetch(url1, {
headers: { Authorization: "Bearer " + ScriptApp.getOAuthToken() }
});
var currentParent = JSON.parse(res).parents[0];
var url2 =
"https://www.googleapis.com/drive/v3/files/" +
sourceFileId +
"?addParents=" +
destinationFolderId +
"&removeParents=" +
currentParent;
UrlFetchApp.fetch(url2, {
method: "patch",
headers: { Authorization: "Bearer " + ScriptApp.getOAuthToken() }
});