Check Google Drive folder file count before HTML form submission

I need help creating a validation system that counts files in a Google Drive folder before processing form data.

I have an HTML form connected to Google Apps Script and I want to add file counting functionality. When users click the Process button, I need the system to count files in a specific subfolder.

The form is connected to a Google Sheets document through GAS. Here’s what I’m trying to accomplish:

  1. Count files in a target subfolder
  2. Apply conditional logic based on file count
  3. Show appropriate user feedback

For the validation logic, I want these conditions:

  • If folder contains exactly 3 files: show confirmation dialog asking “Continue with submission?”
  • If user confirms: add a new file to the folder and proceed
  • If user cancels: block form submission
  • If folder has 4+ files: display error message and prevent submission entirely

I’m looking for guidance on implementing the file counting function and integrating it with the form submission process. The folder structure is nested within the main GAS project folder.

Any help with the Google Drive API calls and form validation would be appreciated.

I dealt with this last year. DriveApp.searchFiles() with a folder ID query beats iterating through getFiles() - way more efficient. Wrap your file counting logic in a function that runs before the form submission handler. For the confirmation dialog, use Browser.msgBox() with the buttons parameter set to Browser.Buttons.YES_NO. Here’s the tricky bit: stopping form submission when conditions aren’t met. Make sure your GAS function returns a boolean that your client-side JavaScript can check. Heads up - file counting gets slow with big folders, so throw in a loading indicator during validation. Also, newly uploaded files might not show up immediately in the folder count because of Drive’s processing delay.

That file counting approach works well, but I hit caching issues when I tried it. Google Drive sometimes gives you stale counts if you’re adding/removing files a lot. I fixed it by adding Utilities.sleep(1000) right before counting, especially after recent uploads. For the conditional logic, I split each validation scenario into separate functions instead of cramming everything into one giant if-else block. One heads up - if your subfolder path changes or gets moved, the whole validation breaks silently. I learned to add error handling around folder access calls to catch missing folders or permission changes.

you can use DriveApp.getFolderById() for your subfolder, then getFiles() to count them. a sample code could be var files = folder.getFiles(); var count = 0; while(files.hasNext()) { files.next(); count++; } - add your if/else for 3 and 4+ files for processing.