title: Google Script - Create a Folder "Year" with sub-folder "Month"
body: <p>Hopefully someone can help me with this. I am wanting to create a script that will archive a sheet every 24 hours so far I have a script made that emails it.</p>
  
  <p>I am wanting to make a script that if the folder does not exist, create a new folder by "Year" and a sub-folder by "month" inside and place a copy of the sheet inside time stamped with the date of the transfer. I am hoping to find a solution to make this an automated script.</p>
  
  <pre><code>function makeCopy() { 
    var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss"); 
    var name = SpreadsheetApp.getActiveSpreadsheet().getName() + " Copy " + formattedDate; 
    var destination = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); 
    var file = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()) 
    file.makeCopy(name, destination); 
  }
  </code></pre>
  
  <p>Cheers.</p>
  
  <p>EDIT</p>
  
  <p>I have made some progress on the script now to make the Folders and sub folders, but is there an easier way then this?</p>
  
  <p>ok I have managed to get the subfolders created is there an easier way to do it? </p>
  
  <pre><code>    function makeFolder() { 
      var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
      var folderYearDate = Utilities.formatDate(new Date(), "GMT", "yyyy");
      var folderMonthDate = Utilities.formatDate(new Date(), "GMT", "MMMM");
      var name = "Archive - " + formattedDate; // file name
      var parent = DriveApp.getFolderById("xxx").getFoldersByName("Archive")
  
      var yearExist = parent.next();
      var yearFolders = yearExist.getFoldersByName(folderYearDate);
      if(yearFolders.hasNext()){
          var yearFolder = yearFolders.next().getId();
          Logger.log('Yes, There is a year folder.')
          //return;
      } else {
          var yearFolder = yearExist.createFolder(folderYearDate).getId();
  
          Logger.log('There was no year folder. But created')
          //return;
      }
      var child = 
  
    DriveApp.getFolderById("1qZh_O3kbDfDEL2QJoU7U1tUudmjSgWVS").getFoldersByName(folderYearDate)
      var monthExist = child.next();
      var monthFolders = monthExist.getFoldersByName(folderMonthDate);
      if(monthFolders.hasNext()){
          var yearFolderExist = yearFolders.next().getName();
          Logger.log('Yes, There is a month folder.')
          return;
      } else {
          monthExist.createFolder(folderMonthDate);
          Logger.log('There was no month folder. But created')
          return;
      }
      }
  </code></pre>
  
  <p>NEW EDIT</p>
  
  <p>I have compiled the new scripts and done what I could to make a copy of the Archive data too by a little work around again if anyone knows of a cleaner way to do it it be much appreciated.</p>
  
  <pre><code>function makeFolder() {
  var ss              = SpreadsheetApp.getActiveSpreadsheet();
  var sheet           = ss.getSheetByName("ARCHIVE");
  var CurrentDate     = new Date();  
  var oldDate         = new Date();
  oldDate.setDate(CurrentDate.getDate()-1);
  var formattedDate   = Utilities.formatDate(oldDate, "GMT", "yyyy-MM-dd");
  var folderYearDate  = Utilities.formatDate(oldDate, "GMT", "yyyy");
  var folderMonthDate = Utilities.formatDate(oldDate, "GMT", "MMMM");
  var name = "Archive - " + formattedDate; // file name
  var parent = DriveApp.getFolderById("xxx").getFoldersByName("Archive")
  var yearExist = parent.next();
  var yearFolders = yearExist.getFoldersByName(folderYearDate);
  if(yearFolders.hasNext()){
      var yearFolder = yearFolders.next()
      Logger.log('Yes, There is a year folder.')
      //return;
  } else {
      var yearFolder = yearExist.createFolder(folderYearDate)
      Logger.log('There was no year folder. But created')
      //return;
  }
  var monthFolders = yearFolder.getFoldersByName(folderMonthDate);
  if(monthFolders.hasNext()){
      var monthFolder = monthFolders.next()
      Logger.log('Yes, There is a month folder.')
  } else {
      var monthFolder = yearFolder.createFolder(folderMonthDate);
      Logger.log('There was no month folder. But created')
  }
  var folderId = monthFolder.getId();
  var resource = {
    title: name,
    mimeType: MimeType.GOOGLE_SHEETS,
    parents: [{ id: folderId }]
  }
  var fileJson = Drive.Files.insert(resource)
  var fileId = fileJson.id
  Logger.log(fileId)
  var destination = SpreadsheetApp.openById(fileId);
  sheet.copyTo(destination);
  var removeSheet = destination.getSheetByName("Sheet1")
  var reName      = destination.getSheetByName("Copy of ARCHIVE");
  reName.setName("ARCHIVE");
  destination.deleteSheet(removeSheet);
  }
  </code></pre>