2
votes

I want to understand the moveFileToFolder script that is listed as an example when you select google drive scripts http://www.google.com/script/start/ (click "Start Scripting" then select "Drive" option in "Create script for" menu)

The code is below. My question is how would I alter this code so that it could be used to move the file "Austin" into the folder "Texas"? This is presuming that the file "Austin" is a google doc which is currently sitting in my main google drive file list and the folder "Texas" is also currently sitting in my main google drive list.

There are a few other posts regarding moving files in drive and I understand the main concepts but I can't seems to successfully procure the file or folder ID's. Any help greatly appreciated?

/**
 * This script moves a specific file from one parent folder to another.
 * For more information on interacting with files, see
 * https://developers.google.com/apps-script/class_file
 */
function moveFileToFolder(fileId, targetFolderId) {
  var targetFolder = DocsList.getFolderById(targetFolderId);
  var file = DocsList.getFileById(fileId);
  file.addToFolder(targetFolder);
};
3

3 Answers

4
votes

now I had the same task and used this code:

  var file = DriveApp.getFileById(fileId)
  var folder = DriveApp.getFolderById(folderId); 
  folder.addFile(file);
0
votes

Check out function getFolderByName in https://gist.github.com/suntong001/7955694 to see how to get folder ID by its name. For files the principle is the same.

0
votes
function addDocToPublic() {
// Create random filename
var fn = "doc" + Math.floor(10000 * Math.random()) + ".doc";
// Create file with very MIME TYPE and return file ID
var fh = DriveApp.createFile(fn, "Das ist das " + fn + " Testfile", MimeType.MICROSOFT_WORD);
// Get first/next Iterator ID ( = folder ID) for Folder (with name PUBLIC)
var fid = DriveApp.getFoldersByName("PUBLIC").next();
// Copy File to Folder (using file and folder IDs
fh.makeCopy(fn,fid);
// Remove Source File
fh.setTrashed(true);
}

... maybe that way ...