Move all root folder files into folder without creating copies - google-apps-script

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());
}
}

Related

Delete all files AND subfolders in a parent folder in Google Drive

I have the below script - which finds all the files in a folder (including files in subfolders) and deletes them, however, I want this script to delete all the folders in the parent folder as well. So basically the entire contents of the parent folder are wiped.
function DeleteInvoices() {
const getAllFiles = folder => {
const files = folder.getFiles();
while (files.hasNext()) {
const file = files.next();
console.log(file.getName());
file.setTrashed(true);
}
const folders = folder.getFolders();
while (folders.hasNext()) getAllFiles(folders.next());
}
var folder = DriveApp.getFolderById("1-2VAPBzEQxCaoN7KGSB_K_peiLIDk32e");
getAllFiles(folder);
}
Any help would be greatly appreciated :)
To trash the files and folders contained in a parent folder, you only need to iterate through the first level
Nested files and folders will be automatically trashed if their parent folder is trashed
The method setTrashed(trashed) is not only available for files, but also for folders
Working sample:
function DeleteAllContents() {
var folder = DriveApp.getFolderById("1-2VAPBzEQxCaoN7KGSB_K_peiLIDk32e");
const files = folder.getFiles();
while (files.hasNext()){
var file = files.next();
file.setTrashed(true);
}
const folders = folder.getFolders();
while (folders.hasNext()){
var folder = folders.next();
folder.setTrashed(true);
}
}

How to log all files inside a folder

I want to navigate into a specific folder and then print all the files inside that specified folder using Google Apps Script:
function listFilesinSpecFolder(){
var files = DriveApp.getFolderById("specific folder id placed here");
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
}
}
When I tried to log this it gives me an error saying that hasNext() function is not a function.
But when I run the below code it gives me no errors...
function folder(){
var folders = DriveApp.getFolders();
while (folders.hasNext()) {
var folder = folders.next();
Logger.log(folder.getName());
}
}
The following script will help you 'to navigate into a specific folder and then print all the files inside that specified folder'
CODE:
function listFilesinSpecFolder() {
var folder = DriveApp.getFolderById('FOLDER ID GOES HERE');
var file;
var contents = folder.getFiles();
while(contents.hasNext()) {
file = contents.next();
Logger.log(file.getName());
}
}
var files = DriveApp.getFolderById("specific folder id placed here");
this does not return files, it will return a folder
instead try something like:
var folder = DriveApp.getFolderById("specific folder id placed here");
var files = folder.getFiles()

Stop the function when the folder doesn't have xlsx files in (Google App scripts)

I have the next function on Google App Script:
function remove(){
var folder = DriveApp.getFolderById('folderID');
var files = DriveApp.getFilesByType(MimeType.MICROSOFT_EXCEL);
while (files.hasNext()){
var file = files.next();
folder.removeFile(file);
}
}
I want stop/break/exit the function when the folder does not have/contains xlsx files.
How I do that?
Greetings
Your function should work fine. Including stopping if there are no more excel files.
But you do need to change this:
var files = DriveApp.getFilesByType(MimeType.MICROSOFT_EXCEL);
(this searches the entire Drive)
to
var files = folder.getFilesByType(MimeType.MICROSOFT_EXCEL);
Here is the code:
function remove(){
var folder = DriveApp.getFolderById(''folder's id'');
var files = folder.getFilesByType(MimeType.MICROSOFT_EXCEL);;
while (files.hasNext()){
var file = files.next();
folder.removeFile(file);
}

duplicate folder and rename file structure

I'm trying to automate my duplicating folder process in google drive. As part of the process, I want to rename files and folders for each new client.
I've adapted some code found previously. It was previously working well, but for some reason now any folders/files that are more than 1 level deep of the root folder come back "undefined" in the replace section of the command.
function duplicatefolder(){
var newclientname = Browser.inputBox('Client Name')
var sourceFolder = "1. Master Client Folder";
var targetFolder = newclientname;
var source = DriveApp.getFoldersByName(sourceFolder);
var target = DriveApp.createFolder(targetFolder);
if (source.hasNext()) {
copyFolder(source.next(), target, newclientname);
}
}
function copyFolder(source, target,client) {
var folders = source.getFolders();
var files = source.getFiles();
while(files.hasNext()) {
var file = files.next();
var newname= file.getName().toString().replace("Master",client)
file.makeCopy(newname, target);
}
while(folders.hasNext()) {
var subFolder = folders.next();
var folderName = subFolder.getName();
var newFolderName = subFolder.getName().replace("Master",client)
var targetFolder = target.createFolder(newFolderName);
copyFolder(subFolder, targetFolder);
}
}
The script also creates the folder in the root directory of google drive. Ideally, I'd like it to be created inside the folder "Clients". How would I add this to the script?
Appreciate the help.
Cheers
Please see attached link hope this is it what you are looking for
https://yagisanatode.com/2018/07/08/google-apps-script-how-to-create-folders-in-directories-with-driveapp/

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);
}
}
}