Skip to content

Instantly share code, notes, and snippets.

@austinsonger
Last active December 2, 2024 23:54
Show Gist options
  • Save austinsonger/802c537485cff8aef2d25863dadcd13f to your computer and use it in GitHub Desktop.
Save austinsonger/802c537485cff8aef2d25863dadcd13f to your computer and use it in GitHub Desktop.
Google App Script - New File Moves files from "My Drive" to a specified folder automatically.
{
"timeZone": "America/Chicago",
"dependencies": {
"enabledAdvancedServices": [
{
"userSymbol": "Drive",
"serviceId": "drive",
"version": "v2"
}
]
},
"oauthScopes": [
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/script.scriptapp",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/documents",
"https://www.googleapis.com/auth/script.storage"
],
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
// ##################### Parameters for Drive Selection #####################
// - useSharedDrive
// - true: Use a shared drive as the source.
// - false: Use "My Drive" as the source.
// - sharedDriveId: Specifies the ID of the shared drive to target.
// - targetFolderId: Specifies the ID of the folder where files will be moved.
//############################################################################
function moveNewFilesToFolder(useSharedDrive = false, sharedDriveId = null, targetFolderId = "YOUR_TARGET_FOLDER_ID") {
const targetFolder = DriveApp.getFolderById(targetFolderId);
const files = useSharedDrive
? DriveApp.getFolderById(sharedDriveId).getFiles() // Fetch files from Shared Drive
: DriveApp.getRootFolder().getFiles(); // Fetch files from My Drive
while (files.hasNext()) {
const file = files.next();
if (shouldMoveFile(file)) {
targetFolder.addFile(file);
if (useSharedDrive) {
DriveApp.getFolderById(sharedDriveId).removeFile(file);
} else {
DriveApp.getRootFolder().removeFile(file);
}
Logger.log(`Moved file: ${file.getName()} to ${targetFolder.getName()}`);
}
}
}
// Helper function to determine whether a file should be moved
function shouldMoveFile(file) {
const createdDate = file.getDateCreated();
const now = new Date();
const timeDiff = now - createdDate;
const fiveMinutesInMillis = 5 * 60 * 1000;
return timeDiff < fiveMinutesInMillis;
}
// Function to process files from My Drive
function runForMyDrive() {
moveNewFilesToFolder(false, null, "YOUR_TARGET_FOLDER_ID"); // Replace with your folder ID
}
// Function to process files from a Shared Drive
function runForSharedDrive() {
moveNewFilesToFolder(true, "YOUR_SHARED_DRIVE_ID", "YOUR_TARGET_FOLDER_ID"); // Replace with shared and target folder IDs
}
// Add a menu for users to choose actions
// function onOpen() {
// const ui = SpreadsheetApp.getUi();
// ui.createMenu("File Mover")
// .addItem("Move from My Drive", "runForMyDrive")
// .addItem("Move from Shared Drive", "runForSharedDrive")
// .addToUi();
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment