Home > Enterprise >  How to change name new creating folder with the subfolders
How to change name new creating folder with the subfolders

Time:03-27

i want creating new folder with new name from cell in the sheet, and i want copy to this folder files and folders with the sourcedestiny.

Could you help me to solve my problem.

It is possible change name newFolder witout change name folders and subfolders- after his creating?

function copyFolderass() {
   const sourceFolder = DriveApp.getFolderById('');
  const destinyFolder = DriveApp.getFolderById('');
  copyFolder(sourceFolder, destinyFolder);
}

function copyFolderContents(source, target) {
  const filesIterator = source.getFiles();
  while (filesIterator.hasNext()) {
  const file = filesIterator.next();
  file.makeCopy(file.getName(), target);
  }
}

function copyFolder(sourceFolder, destinyFolder) {
   var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("nameFile");
   var name = ss.getRange("F4").getValue();

  const newFolder = destinyFolder.createFolder(sourceFolder.getName()); 
  copyFolderContents(sourceFolder, newFolder);
  const foldersIterator = sourceFolder.getFolders();
  while (foldersIterator.hasNext()) {
    const folder = foldersIterator.next();
    copyFolder(folder, newFolder);
  }
}

CodePudding user response:

Create Folders and Copy Files

Reads a sheet to obtain FilesFolder Id FolderFolder Id and new FolderName

It creates the new folder and copies all of the files from the Files Folder to the newly created folder.

It records the new folder id in FolderId Column.

It records all of the file ids in the FileIds column.

It put the date in the created column.

It doesn't do anything for rows that have content in Created

//FilesFolder   FolderFolder    FolderName  FolderId    FileIds Created 
function createFolderAndCopyFiles() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  const shsr = 2;
  let [hA, ...vs] = sh.getDataRange().getValues();
  let idx = {};
  let col = {};
  hA.forEach((h, i) => { idx[h] = i; col[h] = i   1; });
  vs.forEach((r, i) => {
    if (!r[idx["Created"]]) {
      let dfldr = DriveApp.getFolderById(r[idx["FolderFolder"]]);
      let fldr = dfldr.createFolder(r[idx["FolderName"]]);
      let fldrid = fldr.getId();
      sh.getRange(i   shsr, col["FolderId"]).setValue(fldrid);
      let files = DriveApp.getFolderById(r[idx["FilesFolder"]]).getFiles();
      let ids = [];
      while (files.hasNext()) {
        let file = files.next();
        let fid = file.getId();
        file.makeCopy(file.getName(), fldr);
        ids.push(fid);
      }
      sh.getRange(i   shsr, col["FileIds"]).setValue(ids.join('\n'));
      sh.getRange(i   shsr, col["Created"]).setValue(new Date());
    }
  });
}

Spreadsheet

enter image description here

You fill in the stuff in yellow the program fills in the rest and it won't make folders again if the Created Column not empty.

CodePudding user response:

Thx Cooper for the answer, but this is not that what i want to do.

  1. on google drive i have a template of structure od catalogs.
  2. my code is one of the part code of my main code. main code working like this
  • i click send email and then run my code with creating folder from temtlace
  1. when is creating a have name of the template folder but i want just change one part of this line of my code. instead of this line

    const newFolder = destinyFolder.createFolder(sourceFolder.getName());

i want to just like this - the name of the creating folder is from cell F4. the remaining names of the folders may stays like from template.

var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("nameFile");
var name = ss.getRange("F4").getValue();
const newFolder = destinyFolder.createFolder(sourceFolder.setName(name));

but this line change ale name in subfolders i all structure catalog when is creating.

  • Related