add prefix to all files and folders in google drive - google-apps-script

How can I add a prefix or a suffix to all files/ folders in Google Drive using Google Script. The objective is that whenever a users needs to be deleted to add a suffix to all files and folders and transfer to another user so that the documents of the user can easily be tracked at a later stage.
Regards

The File.setName() method in the Drive Service will do what you want. I'd be happy to help further if you post a sample of your code.

Related

Allow Anyone to Upload Files Directly to my Google Drive

I would like to allow my students to send me one or more files directly to my google drive via a form where they would just enter their name and choose the file(s) to send (drag and drop would be great). A folder with their name would be created in a specific folder on my drive and a warning email would be sent to me. Do you think this is feasible with google script? Thanks in advance.
An example here but paying ...
I've checked and Google Forms already allows for a File Upload kind of question (with drag and drop too).
It automatically creates a folder in your Google Drive (altough it doesnt create a folder for every student) (example here) and in the spreadsheet that it automatically generates there is a permanent link to that file (example here).
If that's not what you are looking for and you need the 'Folder per student' feature then yes, you can use google apps script to move files in folders quite easily.

API Changes for Google Drive’s folder structure and sharing models

I used to use below Google Drive API to move my existing file to another drive folder
drive.files().update(fileId=cloudFile['id'],addParents=sharefile,media_body=media).execute()
I just got an email said that the Google Drive folder structure is changed and I'm not allowed to use "addParent" to share filers anymore. I was wondering how can I do this with drive API? I don't want to re-write the entire code, and I'm looking for the simplest way to replace addParents API.
Answer:
Instead of giving a file multiple parents, you can create a shortcut to another Drive file instead (such as a folder).
More Information:
You can use the shortcuts functionality of Google Drive instead of the multiple parents functionality to emulate the kind of behaviour. From the Documentation:
Shortcuts are files that link to other files on Google Drive.
Shortcuts are Drive files with the application/vnd.google-apps.shortcut MIME type, that point to another file or folder on a Google Drive. You can read more about this here.
References:
Create a Shortcut to a Drive File
Find files & folders with Google Drive shortcuts
G Suite and Drive MIME Types

create copy of google file in the same drive directory

I'm looking for a way to create a copy of a given file in the same Google Drive directory using Google Apps Script.
The goal is to iterate through a folder structure looking for files owned by a specific user; when such a file is found [doc, spreadsheet, PDF], create a copy in the same place and delete the original.
The reason for this is that we have files owned by users in other domains and Google currently does not allows the transfer of ownership outside the domain. Using a script like this, could help us solve the problem.
Any ideas?
Thanks,

How do I restore deleted documents from shared Google Drive folders?

A non-privileged Google Drive user has accidentally removed a large number of files from folders shared across an organisation. They do not have permission to delete the files entirely, because they are not the owner. However, users with edit permissions are able to remove a file from a shared folder. This returns the user to the owner, but seems to leave the file orphaned without a parent folder.
The files were owned by various different users.
How do I restore these files to their correct folders? The Google Drive Audit Log does not contain enough information to restore the folders correctly - the parent folder ID is not included with the "Remove from folder" event.
Google Drive is included in the Reports API of the Google Apps Admin SDK. It provides similar information to the Google Drive Audit Log, but with additional metadata. That includes the parent folder ID of files which were removed.
To restore the files you should first query the Reports API for files removed by the user in question over the relevant time period, using the Activities:list method.
Then you'll need to setup a Google Apps service account (which is a little confusing), to allow you to impersonate the owners of the documents that were removed.
Lastly, you can iterate over the event report for the removed files and use the Files: patch method in Google Drive REST API, to re-add the parent ID's to each of the files.
See Gist Using Google Drive API to restore files removed from shared folders
for example of the last step.

Google apps script - Folder as a script container

Is it possible to restrict the reach of a Google Apps Script to a given Google Drive folder?
I'm creating a stand alone apps script that will be hosted as a web app.
The goal of the script is to list all the sub-folders contained inside the folder (the root folder) where the stand alone script is saved and return a JSON with information about those sub-folders and the files that they contain.
notes:
The name or the id of the root folder is unknown as the script will be distributed to Google Drive users and I want to allow them to save the script inside any folder they would like to.
Although the root folder can have any name or any hierarchical position inside the user's Google Drive folders tree, I'm planning on enforcing a predefined naming structure for the sub-folders.
I thought about two approaches to solve this, but I do not know which one is possible and what Classes and Methods to use.
1)The first approach would be to restrict the script reach to it's container folder, therefore the call to DocsList.getFolders(), would return an array of folders objects just for the folders (sub-folders) contained by the root folder.
Question: Does Google Apps Script provide a Class or Method to restrict the reach of a stand alone script, as described above?
2) Get the name or id of the root folder containing the script and using it retrieve the sub-folders and their files.
Question Does Google Apps Script provide a Method to get the name or the id of the folder that contains the script?
Thanks in advance for any help or tip.
None of those are currently possible. You can write a ui to ask the user for the folder. The google picker now supports picking folders.