Reputation: 5
I have a Google Apps Script code for our workplace that is meant to take files uploaded to a single folder, and move them to other folders based on their file name. This code is working just fine when I try it with my personal Google Drive, but fails when I try it on our workplace Team Drive. Gives the error "API call to drive.files.update failed with error: File Not found: (line 11)" Line 11 in this case is the Drive.Files.update line.
Troubleshooting so far: Have ensured that resources > advanced Google Services > Drive API v2 is set to on. Works also when I try it on my personal Google My Drive. Does not work on the workplace Team Drive. Cannot tell if this is because of some admin restriction on the Team Drive, or I need to use a different Drive.files.update code when in a Team Drive.
function moveFiles() {
var dfldrs=['ALT CHG', 'ALT ADJ', 'ALT PMT', 'APX CHG', 'APX ADJ', 'APX PMT','AUR CHG', 'AUR ADJ', 'AUR PMT','BEA CHG', 'BEA ADJ', 'BEA PMT'];//Seven letter prefixes
var ofObj={'ALT CHG':'id','ALT ADJ':'id','ALT PMT':'id','APX CHG':'id','APX ADJ':'id','APX PMT':'id-','AUR CHG':'id','AUR ADJ':'id','AUR PMT':'id','BEA CHG':'id','BEA ADJ':'id','BEA PMT':'id'};//distribution folder ids
var upldFldr=DriveApp.getFolderById('id');
var files=upldFldr.getFiles();
while(files.hasNext()) {
var file=files.next();
var key=file.getName().slice(0,7);
var index=dfldrs.indexOf(key);
if(index>-1) {
Drive.Files.update({"parents": [{'id': ofObj[key]}]}, file.getId());
}
}
}
Upvotes: 0
Views: 527
Reputation: 11
The script transfers all your personal files to a shared disk. Saves the folder structure.
DRIVE_FOLDER_ID = '111aaa'; // Folder ID on the shared drive
function start() {
var files = DriveApp.searchFiles('"me" in owners');
while (files.hasNext()) {
var file = files.next();
// console.info("Old path: ", getFullPath(file));
newPath = fileMoveWithPath(file, DRIVE_FOLDER_ID);
if (newPath)
console.info("New path: ", getFullPath(newPath));
}
}
function fileMoveWithPath(file, root) {
var folders = [],
parent = file.getParents();
// Проходим по иерархии папок текущего файла до корня
while (parent.hasNext()) {
parent = parent.next();
folders.push(parent);
parent = parent.getParents();
}
if (folders.length > 0)
targetPath = makeNewPath(folders, DriveApp.getFolderById(root));
else
targetPath = DriveApp.getFolderById(root);
if (targetPath) {
try {
targetFile = file.moveTo(targetPath);
} catch {
console.error("Error path: ", getFullPath(file));
return;
}
return targetFile;
};
return;
}
function makeNewPath(folders, newroot) {
var f = folders.pop();
var query = "'" + newroot.getId() + "' in parents and title = '" + f.getName() + "' and mimeType='application/vnd.google-apps.folder' "
var targetFolder = DriveApp.searchFolders(query);
if (targetFolder.hasNext())
targetFolder = targetFolder.next()
else
targetFolder = newroot.createFolder(f.getName());
if (folders.length > 0)
return makeNewPath(folders, targetFolder)
else
return targetFolder;
}
function getFullPath(file) {
var folders = [],
parent = file.getParents();
while (parent.hasNext()) {
parent = parent.next();
folders.push(parent.getName());
parent = parent.getParents();
}
if (folders.length) {
return '> /' + folders.reverse().join("/") + '/' + file.getName();
}
return '> /' + file.getName();
}
Upvotes: 0