Creating a Google Apps Script to Manage Folder Contents in Google Drive

Hey everyone! I’m a beginner with Google Apps Script and I’m trying to figure out how to do something specific with folders in Google Drive. Here’s what I want to do:

  1. Empty out a folder (let’s call it ‘Folder A’) without sending the files to trash
  2. Make a copy of another folder (‘Folder B’) with all its contents
  3. Move everything from the copied folder into ‘Folder A’

I’ve been looking around and trying different things, but I can’t seem to get it right. Does anyone have any tips or code examples that could help me out? I’m really stuck and would appreciate any advice!

Thanks in advance for your help!

hey harry, i’ve done something similar before. you’ll wanna use DriveApp to handle folders and files. first, grab folder A and loop through its contents, moving them elsewhere. then use .makeCopy() on folder B and its stuff. finally, move everything to folder A. lemme know if u need more specifics!

As someone who’s worked extensively with Google Apps Script, I can share my approach to your problem. First, for emptying Folder A, I’d use DriveApp.getFolderById() to get the folder, then iterate through its contents with getFiles() and getFolders(). Instead of deleting, I’d move each item to a temporary folder.

For copying Folder B, I’ve found that using the Advanced Drive Service’s Files.copy() method works best for a deep copy. It’s more reliable than the built-in makeCopy() for preserving folder structure.

To move everything into Folder A, I’d recursively go through the copied folder, using moveTo() for each item. Be careful with file limits and quota - I’ve hit those before when dealing with large folders.

One tip: use batch operations where possible to improve performance. It can significantly speed up the process when dealing with many files.

I’ve tackled a similar task before, and here’s what worked for me:

For emptying Folder A, use DriveApp.getFolderById() to get the folder, then iterate through its files and folders with getFiles() and getFolders(). Move each item to a temporary location instead of trashing.

To copy Folder B, use the copyFolder() method from the Advanced Drive Service. This creates a deep copy with all contents.

Lastly, to move everything into Folder A, iterate through the copied folder’s contents and use moveTo() for each item.

Remember to enable the Advanced Drive Service in your script editor for full functionality. Hope this helps point you in the right direction!