5

How to avoid creating a new file by counting the number of files in a folder

 3 years ago
source link: https://www.codesd.com/item/how-to-avoid-creating-a-new-file-by-counting-the-number-of-files-in-a-folder.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to avoid creating a new file by counting the number of files in a folder

advertisements

I have a software on my computer where when i run reports from it auto puts out a folder with 4 csv files. Every time i run a report i get a new folder with 4 new files. These folders and files automatically sync with one of my folders in GDrive. I have the following script set up to take the 4 spreadsheets and combine them into 1 new sheet in the same folder after the file has been uploaded. I am planning on setting up the script to run on a time-based event, it iterates through all its sub folders and combines the files into the 1 file.

I am trying to figure out how to make sure if a file has already been generated for that folder that it skips that folder. I have the following code but it is not working and multiple files are still being generated. My initial thought was to count the basic number of files i have in the folder and if its greater than that number the script should not be ran. I believe i have an error in my if statement code.

/* Head Master Info >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */

/* Top Level Google Drive Folder ID (not absolute root, just the top of the directory
you want to work from)*/
var TopDriveFolderID = "0B2rN5b8fW77ldXZXOXFLZGlSamc";
var BaseNumberOfFilesInFolder = 5;

/* End Head Master Info >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

/* Body >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

/* Functions to iterate through all sub folders */
function listFolders() {
  var parentFolder = DriveApp.getFolderById(TopDriveFolderID);
  var childFolders = parentFolder.getFolders();
  var cnt = 0;

  while(childFolders.hasNext()) {

    var child = childFolders.next();
    //    Logger.log(child.getName() + " |Drive_ID: " + child.getId());
    var newSpreadSheetChildId = child.getId();
    // **the newSpreadSheetChildId Variable is also the folder ID,
    //    hence they are used interchangeably **

    /* SpreadSheet Combiner >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>*/    

    /* Name of combined Spreadsheet*/
    var newSpreadSheetName = DriveApp.getFolderById(newSpreadSheetChildId).getName();

    /* Name of Folder to be looked up*/
    // var masterFolder = "reports";

    /* Retrieve the desired folder */
    //var myFolder = DriveApp.getFoldersByName(masterFolder).next();
    var myFolder = DriveApp.getFolders().next();

    /* Get all spreadsheets that resided on that folder */
    var spreadSheets = myFolder
       .getFilesByType("application/vnd.google-apps.spreadsheet");
    var spreadSheetName = myFolder.getName();
    //    Logger.log(spreadSheetName);

    /* Create the new spreadsheet that you store other sheets */
    var newSpreadSheet = SpreadsheetApp.create(newSpreadSheetName);

    /* Iterate over the spreadsheets over the folder */
    while(spreadSheets.hasNext()) {

      cnt++;
      Logger.log(cnt + "early");

      var sheet = spreadSheets.next();

      /* Open the spreadsheet */
      var spreadSheet = SpreadsheetApp.openById(sheet.getId());

      if (cnt > BaseNumberOfFilesInFolder) {

        /* Get all its sheets */
        for(var y in spreadSheet.getSheets()) {
          Logger.log(cnt + "late");
          /* Copy the sheet to the new merged Spread Sheet */
          spreadSheet.getSheets()[y].copyTo(newSpreadSheet);

          /* In order to move the file to the folder we want, and because
          google considers the SpreadSheet a Google Spreadsheet
          instead of a file, we have to convert the SpreadSheet to a file in
          order to move it. Thats what the next 2 lines of code do.*/
          var getNewSSid = newSpreadSheet.getId();
          var SStoGFile = DriveApp.getFileById(getNewSSid);
          /* Actually moving the file*/
          DriveApp.getFolderById(newSpreadSheetChildId).addFile(SStoGFile);
          /* Deleting the duplicate file that's created in the process*/
          var rootFolder = DriveApp.getRootFolder();
          DriveApp.getRootFolder().removeFile(SStoGFile)

        }
      }  else {
        continue;
      }

      /* End SpreadSheet Combiner >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>*/       

      getSubFolders(child); 

    }

  }

  function getSubFolders(parent) {
    parent = parent.getId();
    var childFolder = DriveApp.getFolderById(parent).getFolders();
    while(childFolder.hasNext()) {
      var child = childFolder.next();
      Logger.log(child.getName());
      getSubFolders(child);
    }
    return;
  }
  /* End iterate through Sub Folders */

}


I think you are right about the if statement. Try changing:

if (cnt > BaseNumberOfFilesInFolder) {

if (cnt >= BaseNumberOfFilesInFolder) {




Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK