Home > Mobile >  google apps script. copy files to one drive separate folders by file names
google apps script. copy files to one drive separate folders by file names

Time:02-16

I would like to upload files from google drive to one drive separate folders based on part of the file name. For instance the files would be: John Doe_story1; John Doe_story2; Jane Doe_story1; Jane Doe_story2; and ect. So these files should be uploaded to separate folders such as "/samplefolder/John Doe/Stories/"; "/samplefolder/Jane Doe/Stories/"; and ect. Currently I have a google apps script to upload files to "/samplefolder". Script is made by the help of @Tanaike and it is as follows:

function myFunction() {
var prop = PropertiesService.getScriptProperties();
var odapp = OnedriveApp.init(prop);
var source = DriveApp.getFolderById("###"); // Please put your folder ID.
var files = source.getFiles();
while (files.hasNext()) {
var file = files.next();
odapp.uploadFile(file.getId(), "/samplefolder/"); // Please set the destination folder name. In this case, the folder name in OneDrive.
 }
}

I would be really appreciated if anyone could help me finalize the script. Thank you in advance for any help.

CodePudding user response:

If the format of the filenames is the constant like For instance the files would be: John Doe_story1; John Doe_story2; Jane Doe_story1; Jane Doe_story2; and ect., how about the following modification?

From:

odapp.uploadFile(file.getId(), "/samplefolder/");

To:

odapp.uploadFile(file.getId(), `/samplefolder/${file.getName().split("_")[0]}/Stories/`);
  • In the library of OnedriveApp, when the folder name is not existing, the folder is created and the file is uploaded to the created folder. When the folder name is existing, the file is uploaded to the existing folder.
  • Related