Each month, I move files from one active folder (Prep) to 5 different folders (Toyota,Honda,Mercedes,General Motors,Ford) in the Google Shared Drive. Each month, a new subfolder for the month (ex: January/February/March/August) is added into each of the 5 folders that were mentioned above. The files from the active folder (Prep) would then be moved to the 5 folders depending on the name of the file. For example: Next month, the file 'Toyota Claims' will be moved to the August subfolder in the Toyota folder, while the 'Ford Complaints' file will be moved to the August subfolder in the Toyota folder.
Each month, the files in the active folder (Prep) will have the same names. The destination folders will also have the same names. The only thing that will be different is that a new month subfolder will be created and the files in the active folder will be moved to the new months subfolder in each Car Manufacturer folder. Since the car manufacturer folders don't change, their folder Ids will always be the same.
Right now, the code (see below) I have only allows me to move the file if I manually find the parent folder id, destination folder id, and file id and input them manually into the macro.
function moveFilesBetweenSharedDrives(parentFolderId, destinationFolderID, fileId ) {
const data = Drive.Files.update({}, fileId, null, {
addParents: destinationFolderID,
removeParents: parentFolderId,
supportsAllDrives: true,
fields: 'title,embedLink',
});
console.log('File Moved', data.title, data.embedLink);
}
moveFilesBetweenSharedDrives("1FhaTIWcQZ-ykXCXnGHgSl59334RYAngr","1setPITRSnsK7yTHs-sL3UepcFNPTaDcv", "1R9gLWEpGjnkHLTgmCISL5QNQJbVU-9QZwNXu44HKCLI")
moveFilesBetweenSharedDrives("1FhaTIWcQZ-ykXCXnGHgSl59334RYAngr","1badWtc7OG4vZ2J-kYvljmB-gH97WKE-Z", "1SiAyMZiPENQjfJSYHREi9b0pq_VBWAu_")
The active folder Id will always be the same. Since the car manufacturer folders don't change, their folder Ids will also always be the same. How can I add conditions to this code that will allow me to select the most recent months folder. Is there a way to use several if statements to find the most recent month present and then save the files in there? Also, how can I modify this code to hardcode the various files names into the script rather than use the file ID to move the file. The file names will be unique in the active folder.
Edited: Here is an image to better show what I'm trying to do: There's files in an active (parent) folder that I want to move to other folders based on the name of the file.
CodePudding user response:
I believe your current situation and your goal are as follows.
There are folders of "Prep" and "Toyota,Honda,Mercedes,General Motors,Ford" in the shared Drive.
- These folder names and IDs are constant.
There are several files like "Toyota Claims", "Ford Complaints" in "Prep" folder.
For example, when this month is July, you want to move the file of "Toyota Claims" to "August" folder in "Toyota" folder. And also, you want to move the file of "Ford Complaints" to "August" folder in "Ford" folder.
You want to achieve this using Google Apps Script.
From your reply of
Yes, the moveFilesBetweenSharedDrives function has worked for me. I was able to successfully move files between two folders in the Google Shared drive
, your showing script ofmoveFilesBetweenSharedDrives
can be used for moving the files in the shared Drive without any errors.
Sample script:
In this sample script, your function of moveFilesBetweenSharedDrives
is used. Before you use this script, please set the variables of srcFolderId
(folder ID of "Prep" folder) and folders
(folder IDs of "Toyota,Honda,Mercedes,General Motors,Ford" folders). And, please enable Drive API at Advanced Google services.
function moveFilesBetweenSharedDrives(parentFolderId, destinationFolderID, fileId) {
const data = Drive.Files.update({}, fileId, null, {
addParents: destinationFolderID,
removeParents: parentFolderId,
supportsAllDrives: true,
fields: 'title,embedLink',
});
console.log('File Moved', data.title, data.embedLink);
}
// Please run this function.
function main() {
// Please set the folder IDs of 6 folders you want to use.
const srcFolderId = "###"; // Please set folder ID of "Prep" folder.
const folders = {
"Toyota": "###", // Please set folder ID of "Toyota" folder.
"Honda": "###", // Please set folder ID of "Honda" folder.
"Mercedes": "###", // Please set folder ID of "Mercedes" folder.
"General Motors": "###", // Please set folder ID of "General Motors" folder.
"Ford": "###", // Please set folder ID of "Ford" folder.
};
// Retrieve next month as a text.
const date = new Date();
date.setMonth(date.getMonth() 1);
const nextMonth = Utilities.formatDate(date, Session.getScriptTimeZone(), "MMMM");
// Retrieve files in "Prep" folder.
const params = { corpora: "allDrives", includeItemsFromAllDrives: true, supportsAllDrives: true, fields: "items(title,id)" };
const keys = Object.keys(folders).map(e => e.toLowerCase());
const fileObj = Drive.Files.list({ q: `'${srcFolderId}' in parents and mimeType != '${MimeType.FOLDER}' and trashed=false`, ...params }).items.reduce((o, { title, id }) => {
const temp = keys.find(e => title.toLowerCase().includes(e));
if (temp) o[temp] = o[temp] ? [...o[temp], id] : [id];
return o;
}, {});
// Move files from the source folder to the destination folders.
Object.entries(folders).forEach(([name, id]) => {
const dstFolder = Drive.Files.list({ q: `'${id}' in parents and mimeType = '${MimeType.FOLDER}' and trashed=false`, ...params }).items.find(({ title }) => title == nextMonth);
const srcFiles = fileObj[name.toLowerCase()];
if (dstFolder && srcFiles && srcFiles.length > 0) {
srcFiles.forEach(srcFileId => moveFilesBetweenSharedDrives(srcFolderId, dstFolder.id, srcFileId));
}
});
}
- When this script is run, the files are retrieved from "Prep" folder, and by searching the destination folder of the month, those files are moved.
Note:
In this case, if you cannot move the files by permission, an error occurs. Please be careful about this.
If an error occurs when the files are moved in your script of
moveFilesBetweenSharedDrives
, please modify it as follows, and test it again.function moveFilesBetweenSharedDrives(parentFolderId, destinationFolderID, fileId) { Drive.Files.copy({parents: [{id: destinationFolderID}], title: DriveApp.getFileById(fileId).getName()}, fileId, {supportsTeamDrives: true}); Drive.Files.remove(fileId, {supportsAllDrives: true}); console.log('File Moved', fileId); }