google_drive

Managing Files in Google Drive App

This tutorial is basically about Google Drive App Script.

Firstly, App Script is basically Javascript in the cloud, you can write your own Javascript code and have it run on Google cloud and access Google services.

App Script is javascript so you probably already know the program syntax, it is not a new language to learn, but you need to learn the services Google have and how to use them.

However, one of the Google services is the Drive service which allows scripts to create, find and modify files and folders in google drive backup.

Here, we will learn how to start with app script and how to use app script to create files in the drive, delete it, check its properties and share.

So, if you wanna know more about apps script you can visit Google App Script page https://developers.google.com/apps-script/
Therefore, this is the main page of google app script documentation, it has all the details to get started with App Script.
Similarly, it has tutorials, all the news, and updates on all the new features of the product and has all the details about all the services and functions you can use.

Write your Script

To write your app script you need to go to https://script.google.com and click on New Script. Which will open a script editor page where you can write your scripts.

Script to Loop Through All Files in Google Drive Folder

In my Drive I have a folder ‘TestFolder’ which contains four image files as you can see in image below.

Google Drive with Apps Script

Following code loop through each image file in ‘TestFolder’ and write their names to script editor logger window.

[cc lang=”javascript” escaped=”true” width=”100%” theme=”blackboard” noborder=”1″ line_numbers=”off”] function logFolderFileNames() { var folder=DriveApp.getFolderById(‘1wxEV1uvBsy-u8HCIdaX64MESDvhS8zjt’); var files=folder.getFiles(); while(files.hasNext()){ var file=files.next(); Logger.log(file.getName()); } } [/cc]

Hence, after executing the script you can see the output in log window

Move a File From One Folder to Another Folder

This function move file ‘Pic2.jpg’ from one folder to another folder.

[cc lang=”javascript” escaped=”true” width=”100%” theme=”blackboard” noborder=”1″ line_numbers=”off”] function moveFile(){ //Source Folder which contains the file var parentFolder=DriveApp.getFolderById(‘1wxEV1uvBsy-u8HCIdaX64MESDvhS8zjt’); //file which need to be moved to another folder, suppose you need to move ‘Pic2.jpg’ file var childFile=parentFolder.getFilesByName(‘Pic2.jpg’).next(); //Get Destination folder where file will be moved. var destinationfolder=DriveApp.getFolderById(‘1dd0FiAgV2dVpma-Pw6XjwVdm1AuCeYlU’); //move file to folder destinationfolder.addFile(childFile); //Remove the file from parent folder parentFolder.removeFile(childFile) } [/cc]

Create Files in Drive

Now, let’s write script to create a file in drive, 
App Script has three methods to create files in Drive.

  • DriveApp.createFile(blob);
  • DriveApp.createFile(name, content);
  • DriveApp.createFile(name, content, mimeType)

Create File from Blob Result

[cc lang=”javascript” escaped=”true” width=”100%” theme=”blackboard” noborder=”1″ line_numbers=”off”] // This will create an image file in root folder of drive and set its name as ‘File Create From Blob Result’, it uses the Google Map Service to create the image var blob = Maps.newStaticMap().setCenter(‘Delhi, India’).getBlob(); DriveApp.createFile(blob).setName(‘File Create From Blob Result’); [/cc]

Create File from two parameters, name and content

[cc lang=”javascript” escaped=”true” width=”100%” theme=”blackboard” noborder=”1″ line_numbers=”off”] //This will create a new file in root folder of drive var file=DriveApp.createFile(‘Drive File’, ‘This is test file created in google drive using app script’); [/cc]

Create File using three parameters, name, content and mimeType

[cc lang=”javascript” escaped=”true” width=”100%” theme=”blackboard” noborder=”1″ line_numbers=”off”] //This create a new html file in drive var file=DriveApp.createFile(‘New File with three parameters’, ‘New File in drive create by passing three parameters, name, content and mimitype‘, MimeType.HTML); [/cc]

Deleting Files From Drive

Following function send file trash folder.

[cc lang=”javascript” escaped=”true” width=”100%” theme=”blackboard” noborder=”1″ line_numbers=”off”] function deleteFile() { var file=DriveApp.getFileById(‘1QO1HVuldcRvKILAY2GVPXsTZFhbo6E-r’); file.setTrashed(true); } [/cc]

Look through previous post Google Apps Script to create and delete folder in Google drive to delete files permanently.

Sharing Files

For sharing files you can use below methods.

I hope Now you can create/delete and share files in the Google Drive with App Script, Let me know for any query in comment box or email me at info@xcript.org

Thanks for Visiting!

Add a Comment

Your email address will not be published. Required fields are marked *