file.MoveTo doesn't move a file in a shared folder - google-apps-script

I'm trying to move a file in a shared folder which I have editor access to a folder in my drive, but I can't. I can move the file to a different folder in my drive, though. The code is simple: file.moveTo(toFolder); Am I doing a wrong thing? How can I move a file in a shared folder to my folder. Here is my code:
function sendJudgeReqCN() {
var fromFolderId = "x";
var toFolderId = "y";
var saveFolderId = "z";
copyAndSaveAllFilesInFolder(fromFolderId, toFolderId, saveFolderId);
}
function copyAndSaveAllFilesInFolder(fromFolderId, toFolderId, saveFolderId) {
var fromFolder = DriveApp.getFolderById(fromFolderId);
var toFolder = DriveApp.getFolderById(toFolderId);
var saveFolder = DriveApp.getFolderById(saveFolderId);
var files = fromFolder.getFiles();
while (files.hasNext()) {
var file = files.next();
file.makeCopy(toFolder);
file.moveTo(saveFolder);
var fileName = file.getName();
Logger.log(fileName);
}
}

It is not enough to be editor in order to move a folder out of a shared Drive to your personal drive
You need to be organizer or owner to perform this kind of operation
This behavior is in agreement with the one of the Drive UI, wher only Manager but not Content Manager can move content from a shared drive to their own drive
Additional information here

Related

How to download a file from Google Drive to my computer using Google Apps Script?

I am trying to download a file from a folder in Google Drive to a folder in my local PC using Google Apps Script. However, I couldn't find a proper way to do it. The files are usually .pdf, .csv, or .docx. And I want to save them to a specific folder in my local computer with a trigger, so that the script will work even when my computer is in sleep. Is this even possible?
I am also okay for a solution that works when my PC is open.
For now I am able to reach the files but couldn't find a way to download them to my local.
function downloadFiles() {
var driveApp = DriveApp
var folderIter = driveApp.getFoldersByName('DownloadTrys')
var folder = folderIter.next()
var filesIter = folder.getFiles()
var localFolder = "C:\\Users\\XXXX\\Desktop\\personal\\"
//var file = DriveApp.getFileById("downloadthis.txt");
while(filesIter.hasNext()){
var file = filesIter.next();
var fileName = file.getName();
Logger.log(fileName);
var blob = file.getBlob();
var filePath = localFolder + fileName
Logger.log(filePath)
var data = Utilities.base64Encode(blob.getBytes());
var localfile = Utilities.newBlob(Utilities.base64Encode(data), blob.getContentType(),filePath)
driveApp.createFile(localfile).setName(fileName).setTrashed(true);
};
}```

how to save file to shourcut folder

I am trying to save attachment file from Gmail to Drive
I found a code sample here and its works BUT I need to save the file into the shared folder. at MyDrive I have a shortcut to the shared folder.
for example:
var GDRIVE_FILE = '_sheredFolder_/one/$name
function processThread(thread, label){
var attachments = message.getAttachments();
for(var i=0; i<attachments.length; i++) {
var attachment = attachments[i];
var info = {
'name': attachment.getName(),
'ext': getExtension(attachment.getName())}
var file = createFilename(GDRIVE_FILE, info);
saveAttachment(attachment, file);
}
If you have a folder shortcut in your "My Drive", then it's very likely that the folder is owned by someone else.
In order to be able to add files to a folder owned by someone else you should it should be shared with you as "editor". If it's shared with you as "viewer" you will not able to add the file to that folder.
Related
Moving Files In Google Drive Using Google Script
file.MoveTo doesn't move a file in a shared folder
Well after digging a while, I modify the core to support shortcuts or Shared folders
I add % at the begging of the initial path to identify when its a shared folder or local folder
that's the code:
var GDRIVE_FILE = '%TheSharedFolder/FOLDER/$name';
function getOrMakeFolder(path) {
var folder = DriveApp.getRootFolder();
var names = path.split('/');
while(names.length) {
var name = names.shift();
if(name.charAt(0)==='%')
{
var first = name.substring(1,name.length)
var folders = DriveApp.searchFolders('sharedWithMe');
while (folders.hasNext()) {
var folder = folders.next();
if(folder.getName()===first)
{
Logger.log("folder name is : "+folder.getName()+" folderID: " + folder.getId())
folder = DriveApp.getFolderById(folder.getId())
}
}
continue;
}
if(name === '') continue;
var folders = folder.getFoldersByName(name);
if(folders.hasNext()) {
folder = folders.next();
} else {
folder = folder.createFolder(name);
}
}
return folder;
}

Google App Script File class method getOwner() Always returning null for Google shared Drive files

I am trying to extract file details by using the below app script. It works for all the files saved in my drive or files shared with me by someone else. For shared drives files, var owner = file.getOwner() always returns null. Other than getOwner() all the methods works well even with a shared drive folder. Please suggest if I am doing something wrong here.
Code:
function list_all_files_inside_one_folder_without_subfolders(){
var sh = SpreadsheetApp.getActiveSheet();
var folder = DriveApp.getFolderById('MyFolderID'); // I change the folder ID here
var list = [];
list.push(['Name','ID', 'URL','Size','Upload Date', 'Owner']);
list_all_folders_of_folder(folder,list)
sh.getRange(1,1,list.length,list[0].length).setValues(list);
}
function list_all_folders_of_folder(folder,list){
var subfolder = folder.getFolders();
while (subfolder.hasNext()) {
folder = subfolder.next();
list_all_files_of_folder(folder,list)
}
}
function list_all_files_of_folder(folder,list){
var files = folder.getFiles()
while (files.hasNext()){
file = files.next();
var row = []
var owner = file.getOwner()
Logger.log(file.getName())
Logger.log(owner)
row.push(file.getName(),file.getId(), file.getUrl(), file.getSize(), file.getLastUpdated(), owner.getName() )
list.push(row);
}
}
Kind of expected since using Shared Drive transfers the ownership of the file to the entire organization (Not a user or an email address) and file ownership is removed from the user.
There's a workaround I think that can be implemented but it would require a direct call to DriveAPI to check for file activity (Who created the file) but this is far beyond basic scripting or using advanced Workspace Services on GAS.

Do not make copy of existing file in destination folder

I've been trying to copy contents from one folder to another folder in google drive using google app script. With some online help and documentation of google script, I was able to copy files from one folder to other but it copies all files from source folder to destination folder on time trigger, but I want only the new files to get copied in my destination folder and skip the present files that are already same in my source and destination folder.
I tried to check files by name before copying but couldn't find a way to compare files by name in two different folders of gdrive.
function CopyFiles() {
var SourceFolder = DriveApp.getFolderById('Sid');
var SourceFiles = DriveApp.getFolderById('Sid').getFiles();
var DestFolder = DriveApp.getFolderById('Did');
var DestFiles = DriveApp.getFolderById('Did').getFiles();
while (SourceFiles.hasNext())
{
var files = SourceFiles.next();
var dfiles = DestFiles.next();
if ( files == dfiles){
file.setTrashed(true);}
else{
var f = files.makeCopy(DestFolder);
}
}
}
What I want to achieve is that Script compares files by name in destination folder and if files by that name already exist than skip else, create a copy of that new file in the destination folder.
Copies files from source to destination if destination does have the same file names
In this version no files get trashed.
function CopyFiles() {
var srcFldr=DriveApp.getFolderById('srcId');
var srcFiles=srcFldr.getFiles();
var desFldr=DriveApp.getFolderById('desId');
var desFiles=desFldr.getFiles();
var dfnA=[];
while(desFiles.hasNext()) {
var df=desFiles.next();
dfnA.push(df.getName());
}
while(srcFiles.hasNext()) {
var sf=srcFiles.next();
if(dfnA.indexOf(sf.getName())==-1) {
sf.makeCopy(sf.getName(),desFldr);
}
}
}

Move all root folder files into folder without creating copies

Is there a way to move all files inside the root of Google Drive into a specified folder such as shown below without creating copies of the to be copied files?
Try this -
function myFunction() {
var files = DriveApp.getRootFolder().getFiles();
while (files.hasNext()) {
var file = files.next();
var targetFolder = DriveApp.getFolderById('FolderIDGoesHere').addFile(file);
DriveApp.getRootFolder().removeFile(file);
Logger.log(file.getName());
}
}