Google Drive apply label to all files in folder/subfolder - google-drive-api

My organization is requiring that all files in Drive have one of several labels applied to them. It seems you can multi-select up to 100 files and apply a label, but is there any way to apply a label to all files in a folder/subfolder? Our folders are organized alphabetically, so there are only a handful of files in each subfolder, which leads to a lot of extra manual work going in and out of each folder to apply the label rather than just propagating it down to all files in the folder structure.

If you can identify conditions based on which these files will be labeled, you could use DLP rules (https://support.google.com/a/answer/9655387?hl=en&ref_topic=9646660)
For example if you want to label as Confidential all files which include in the filename the world "Payments", you can create a rule with a regular expression like:
(\W|^)[Pp]ayment[s]{0,1}(\W|$), and assign the label "Confidential" automatically ,to existing and new files.

Related

Moving a file or folder with DriveApp

Really, there is no way to move a file or folder using the DriveApp class?
From what I could gather on the docs and on the StackOverflow answered questions all proposed implementations seem to copy the file to another location and then delete the original file. That would result in at least two problems:
File/folder having a different folderId on the destination;
File/folder being duplicated and stored indefinitely on Google Vault by retention policy.
I must be doing something wrong. Why is there not a method to simply move the file/folder to another destination (as provided by the Drive Web UI)?
Thanks,
When it comes to moving files with DriveApp, you need to work from the Folder rather than the File.
Open the folder you want to move the file to and use the "addFile(file)" method to add the file to that folder, then open the folder you want to move the file from and use "removeFile(file)" to remove it.
This might seem a bit clunky but it's actually possible to have a file in more than one folder at a time on Drive. The folders are really just labels. When considering a file that is already in multiple folders, the meaning of "moving" it to a new folder is kind of ambiguous, it's really a matter of adding/removing it from folders.
Add:
https://developers.google.com/apps-script/reference/drive/folder#addfilechild
Remove:
https://developers.google.com/apps-script/reference/drive/folder#removeFile(File)
Stumbled across this post (two years & 6 months later) and just wanted to point out that its now possible to do this:
DriveApp.getFileById(myFileId).moveTo(DriveApp.getFolderById(myTargetFolderId));

Is multiple root level directories upload possible with webkitdirectory?

We have recently developed a mass uploader that uses the webkitdirectory functionality in order to be able to upload directories and parse all inner files.
However the uploading of multiple root directories is not possible even after using multiple attribute.
In Mozilla's documentation, multiple is mentioned & used in the examples.
In this example, a directory picker is presented which lets the user choose one or more directories. When the change event occurs, a list of all files contained within the selected directory hierarchies is generated and displayed.
But basically is useless. With or without multiple produces the same result. We're only able to select 1 directory.
How can we have the input with webkitdirectory allow us to select multiple directories? I would truly appreciate any ideas or hacks.

I want to find some files that includes some keyword and copy it to other folder

I want to find files that includes some keyword and copy it to other folder.
So I need two features.
Find files by keyword and list it at "file" type value.
copy the file to different directory.
But Google drive supports same folder name so I don't know how to targeting folder by name. And I want copy the file only if there is not file has same name. Because I'll run this script every 5 minutes and if I don't set about that, there are many same name files to target directory.
How can I do this?
AFAIK, the only supported methods that you can use to get files in Class DriveApp are:
getFileById(id)
getFiles()
getFilesByName(name)
getFilesByType(mimeType)
Then, to make a copy to other folder, you can use the following methods that are given in Class File
makeCopy(destination)
makeCopy(name, destination)
Lastly, the suggested solutions given in this SO post might also help.

How do I perform a function on ALL the children (and subchildren, etc) of a single folder?

I have written a (successful) function which takes a Google Doc as its input, and makes certain changes to its body text.
Here's where I'm stumped: how do I perform THAT function on ALL the Google Docs in a given folder? When I say "all," what I mean is:
I want to INCLUDE the folder's children, sub-children, sub-sub-children. (I tried using myFolder.getFiles(), but this is too narrow: it only gets the docs that are the direct children of the folder -- not the sub-children, etc.)
I want to OMIT any files that are NOT in this folder. (I tried using DriveApp.getFiles(), but this is too broad: it gets ALL the docs in my Drive.)
Thanks!
The relation in Google Drive is from children to parent. This means that each file has a parameter called "Parents" where it stores the id of the folders where it belongs. Instead of the parent having a list of children.
When retrieving the list of files from a folder you are selecting all the files that have that folder id in the 'parents' parameter.
Because of this you won't be able to query all the children belonging to a parent folder.
You will have to get all the children from a folder, and look for the folders in it and then get the children for each one of those folders and so on.

customizing `org-publish-project-alist`

I'm trying to publish webpage using org-mode. Two questions:
Is there a way to "sync" the org-mode files in the base-directory and the html files in the publishing-directory? Specifically, if I delete an org file in the base-directory, can I get org-publish-html to delete the corresponding file in the html directory also?
If I have pages within subdirectories, how can I specify a single .css file in the root directory to be used for the style sheet? For instance, my directory structure is as follows:
public_html/
css/
mystyle.css
index.html
subdir/
index.html
With the following specifications in org-publish-project-alist (this is just a subset) --
:publishing-directory "public_html"
:style "<link rel=\"stylesheet\" href=\"css/mystyle.css\" type=\"text/css\"/>"
mystyle.css is used by public_html/index.html but not by public_html/subdir/index.html. Is there a simple remedy to this (I want the style sheet to be used by both/all files in subdirectories)?
Thanks much ~
There is no straightforward way of doing this. Org-mode doesn't know (or care) about the location to which it is publishing - it just sends things there and makes sure the correct directory structure exists. There is a hook in the publishing process that gets called after the files have been pushed to their published location. This is controlled by setting the :completion-function property in your org-publish-project-alist. You could use this hook to write a function that compares the *.org files in your base-dir and subdirectories to the accompanying *.html published files, and remove those *.html files that don't have an accompanying *.org file.
I suspect this will be most easily accomplished by making your Lisp completion-function call a shell script that removes the necessary files. If you are doing something fancy with the :include, :exclude, or :base-extension properties, you'll likely want your completion-function to grab the pertinent information from the plist and then pass them to your shell script. This org-mode page has an example completion-function that shows how to get property values for the org-publish-project-alist. You would then need to pass them to your shell script.
There are several ways to do this. Perhaps the simplest is to just override the default style sheet in each file with a line such as:
#+STYLE: <link rel="stylesheet" type="text/css" href="../stylesheet.css" />
for your first level of subdirectory files, and keep adding ../ as you get deeper in the directory structure.
Another possibility is generate generic template files for each level within the directory tree. This org-mode page gives a nice example of how to set this up.
Lastly, another option is to use the :preparation-function property of org-publish-project-alist to define a function that will automatically change the style file for each file. Again, this is probably best done by having the Lisp preparation-function call a shell script to parse the files. I could imagine doing this with the Unix sed program to find a regular expression denoted something like href="#MYLOC#/stylesheet.css" /> and substitute the stuff between #'s with the appropriate level within the directory tree. This seems like overkill, given the other options.