

How to avoid creating a new file by counting the number of files in a folder
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.

How to avoid creating a new file by counting the number of files in a folder
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
-
52
You can set a file or folder to be “ignored” by Dropbox. This allows you to organize files and folders in the Dropbox folder on your computer without storing them on dropbox.com or on the Dropbox server at all. Onc...
-
14
use-count-up React/React Native component and hook to animate counting up or down to a number. Key features :trophy: Lighter implementation and smaller bundle size
-
15
How do I save a file in the top-level folder? advertisements the structure of folders in server look like this: www.example.com
-
5
Open folder or file with Sublime Text from Finder toolbar Post author:herrkaefer Post published:01/19/2020...
-
4
Counting the number of matching characters in two ASCII strings Suppose that you give me two ASCII strings having the same number of characters. I wish to compute efficiently the number of matching characters (same position, sa...
-
9
Find the file in the folder with a specific ruby end advertisements I have many files in the folder, such as mp4, mov, avi files. But I have...
-
3
The No Title® Tech BlogTales of an exploration on antigravity and other potentially unrelated matters New project: counting...
-
8
Counting Word Frequency in a File Using Python Ever wondered about a quick way to tell what some document is focusing on? What is its main topic? Let me give you this simple trick. List the unique words mentioned in the document, an...
-
3
The Changelog Load testing a $4 VPS, TOML for .env files, counting unique visitors sans cookies, the Arc browser & a love letter to Deno...
-
7
How to avoid emails going to the spam folder – words to avoid and more By Sean Tinney November 30, 2023 Let’s fac...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK