Skip to content

Instantly share code, notes, and snippets.

@JakubAndrysek
Last active May 29, 2024 11:02
Show Gist options
  • Save JakubAndrysek/d3c16b85bf34eaca18d33df471104dd5 to your computer and use it in GitHub Desktop.
Save JakubAndrysek/d3c16b85bf34eaca18d33df471104dd5 to your computer and use it in GitHub Desktop.
Copying files and folders from a personal Google Drive to a Shared Google Drive (aka Team Drives) using Google Apps Script

Copying files and folders from a personal Google Drive to a Shared Google Drive (aka Team Drives) using Google Apps Script

  1. Copy app script
  2. In setting enable Show appscript.json ...
  3. Add "oauthScopes": ["https://www.googleapis.com/auth/drive"] on the end of the appscript.json file.
  4. Edit sourceFolderId and targetSharedDriveId based on your Google Drive web app URL links
    • https://drive.google.com/drive/folders/dfsfsdt453rfds45341243-53425 -> dfsfsdt453rfds45341243-53425

References

{
"timeZone": "Europe/London",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"oauthScopes": ["https://www.googleapis.com/auth/drive"]
}
/**
* Copying files and folders from a personal Google Drive to a Shared Google Drive (aka Team Drives) using Google Apps Script
* Author: Jakub Andrýsek + https://stackoverflow.com/users/8199076/azmol
* Website: https://kubaandrysek.cz
* Email: email@kubaandrysek.cz
* GitHub: https://github.com/JakubAndrysek
* License: MIT
* File: https://gist.github.com/JakubAndrysek/d3c16b85bf34eaca18d33df471104dd5
*/
function copyDriveStructure() {
var sourceFolderId = 'dfsfsdt453rfds45341243-53425-random-ID'; // update ID
var targetSharedDriveId = 'vdsf7fds98dfs-7dsf78dfs-random-ID'; // update ID
var sourceFolder = DriveApp.getFolderById(sourceFolderId);
var targetDrive = DriveApp.getFolderById(targetSharedDriveId);
// copyFolder(sourceFolder, targetDrive, false); // Copy the entire folder - including files
copyFolder(sourceFolder, targetDrive, true); // Only print folders and files
}
function copyFolder(sourceFolder, targetFolder, onlyPrintFiles = true) {
// Copy the folders
if (onlyPrintFiles) {
Logger.log('Only print file names');
}
var sourceSubfolders = sourceFolder.getFolders();
while (sourceSubfolders.hasNext()) {
var sourceSubfolder = sourceSubfolders.next();
Logger.log(`Creating folder: ${sourceSubfolder.getName()}`)
if (!onlyPrintFiles) {
var newFolder = targetFolder.createFolder(sourceSubfolder.getName());
// Copy the files
copyAllFilesInFolder(sourceSubfolder, newFolder, onlyPrintFiles);
}
copyFolder(sourceSubfolder, newFolder); // Recursive call for subfolders
}
}
function copyAllFilesInFolder(
sourceFolder, targetFolder, onlyPrintFiles = true) {
var sourceFiles = sourceFolder.getFiles();
while (sourceFiles.hasNext()) {
var sourceFile = sourceFiles.next();
Logger.log(`- adding file: ${sourceFile.getName()}`)
if (!onlyPrintFiles) {
sourceFile.makeCopy(sourceFile.getName(), targetFolder);
}
}
}
@zapCip
Copy link

zapCip commented May 29, 2024

function copyFolder(sourceFolder, targetFolder, onlyPrintFiles = true) {
// Copy the folders
if (onlyPrintFiles) {
Logger.log('Only print file names');
}
copyAllFilesInFolder(sourceSubfolder, newFolder, onlyPrintFiles);
var sourceSubfolders = sourceFolder.getFolders();
while (sourceSubfolders.hasNext()) {
var sourceSubfolder = sourceSubfolders.next();
Logger.log(Creating folder: ${sourceSubfolder.getName()})
var newFolder = targetFolder.createFolder(sourceSubfolder.getName());
copyFolder(sourceSubfolder, newFolder, onlyPrintFiles); // Recursive call for subfolders
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment