Google Apps Script to Download Files,Images,PDF From URL to Google Drive Full Project For Beginners

 

 

First of all you need to go to the script.google.com url and then make a new project like this as shown below

 

 

 

 

 

And now copy paste the below code inside the editor

 

 

code.gs`

 

 

function downloadFile(){

  var filename = ""

  var filesize = ""

  var url = "https://freemediatools.com/img/profile.jpg"

  var response = UrlFetchApp.fetch(url)

  var rc = response.getResponseCode()

  var folderId = "1XsVZoyADqioGGMsFSuK27iqBSqbnaSY2"


  if(rc == 200){

     var fileBlob = response.getBlob()

     var folder = DriveApp.getFolderById(folderId)

     if(folder != null){

       var file = folder.createFile(fileBlob)

       filename = file.getName()

       filesize = file.getSize()

     }

  }


var fileInfo = {'rc':rc,'filename':filename,'filesize':filesize}

Logger.log(fileInfo)


}

 

 

In the above code we have written the function downloadFile in which you need to replace the folderId and url of the file which needs to be downloaded. In order to get the google drive folder id you need to follow below steps as shown below

 

 

 

 

 

 

 

 

 

 

 

Running the Google Apps Script

 

See also  Shell Script to Find Factorial of Number Using For & While Loop on Command Line Full Tutorial For Beginners

 

In order to run the script you need to first of all save the code and project by a name as shown below and then hit the run button and grant the permissions as shown below

 

 

 

As you can see the image file has been downloaded inside the folder of google drive using google apps script and you can repeat for any url file such as pdf,video and text,image file

 

Leave a Reply