In google drive API, is there a way to find all the image files in multiple folders? - google-drive-api

In google drive API, is there a way to find all the image files in multiple folders?
I used 'mimeType' and 'in parents' with 'or' in my query (q).
However it does not work.
mimeType checking is working fine; however, if I put more than one 'in parents' grouping with 'or', it does not return any files.
I was trying to do something like this:
q would be like ('folderid' in parents or 'filderid2' in parents) and (mimeType = 'image/').
I might have mistyped (mimeType = ...) part here, but this part is working fine in my code. I just need to check 'folderid' in parents... part.
Thank you!

Related

Get list of file for a user excluding sharewithme? [duplicate]

Any variation of 'sharedWithMe=false' gives me the 400 error 'Invalid Value' in reference to the q parameter:
sharedWithMe=false
sharedWithMe!=true
not(sharedWithMe=true)
etc...
It's annoying, I want to get a list of all my files using the 'allDrives' corpora (User files + Shared Drives + Shared with me) but not include the files that have been shared with me.
There's simply no way to do this without breaking it down into separate requests (1 for root Drive with 'me' in owner query, and a request for each shared drive I'm a member of.
Does anyone know why it's doing this, or can suggest an elegant solution around this?
This is a bug
It seems that the documentation doesn't reflect the actual behavior for the sharedWithMe Search Query Term.
Here is a recent tracker that I found which reports this issue:
https://issuetracker.google.com/issues/173111277
Feel free to star it to indicate that you are affected too.
Reference
Subscribing by starring an Issue
Actually it is a bug that you can solve with this workaround:
Instead to use "sharedWithMe=false", consider to use "'me' in owners", even in "q" parameter

Which query string for DriveApp.searchFiles() mimics the Drive search "is:unorganized"?

I would like to recreate in Google App Script the search "is:unorganized", which would be entered in the Google Drive search bar. My goal is to find orphaned files in a manner other than finding the parents of all files and selecting those which are 'null'.
According to the documentation the queries are written according to these rules.
However, it's not clear to me how to test for "unorganized". I've tried:
var orphans = DriveApp.searchFiles(q)
with the following qs
"unorganized=true"
"'unorganized' in parents"
For the first, I get the error "Invalid argument: q" because the query is invalid, and in the 2nd I get an error saying the ID doesn't exist because there is no folder called "unorganized".
Does anyone know the q that corresponds to "is:unorganized"?
Thanks.
My goal is to find orphaned files in a manner other than finding the parents of all files and selecting those which are 'null'.
There's no way currently in DriveApp.searchFiles(query) to compose a query that reproduces the is:unorganized Drive UI search. That's because the Drive API (DriveAPP uses V2) search fields and operators don't give you the breath of functionality required.
There is however a search that will improve upon a scan of all files, and that is using a search query as follows:
var potentialOrphans = DriveApp.searchFiles('not "root" in parents');
This gives you all files except those that are children of My Drive. This is a smaller starting list that you could potentially then iterate through to find those files that have no parents.

files.list() returns an incomplete list when searching for q='FOLDER_ID' in parent

I am trying to gather all of the files and folders that are descendants of a given folder.
To do this I use file.list() with q="'FOLDER_ID' in parent" and trashed=false with FOLDER_ID being the ID of the folder I am interested in. As I process the results I keep track of all of the folders that get returned from this request and then repeat the files.list() call using the new folders in the q parameter. I combine multiple folders in one request by using or and continue to repeat this until no new folders are returned.
Example:
Initial Request: q="('FOLDER_ID' in parent) and trashed=false"
All Subsequent Requests: q="('FOLDER_ID_1' in parent or 'FOLDER_ID_2' in parent or 'FOLDER_ID_3' in parent ...) and trashed=false"
(For more information about how to create queries see Drive REST API - Search for Files)
Sometimes this returns all the folders it should and other times some are left out. This doesn't happen if I remove the q parameter as every single file and folder are returned, none are missing.
After some testing/trial and error, I discovered that if I am not receiving all the folders I should be, sending a request with no q seems to "fix" the problem. The next time I run my application and it uses q, all the correct folders do get returned.
Other Information:
It is not a permissions issue, I am using drive.readonly
It is not a pageSize issue as I have tried different values for this and get different results.
It is not a pageToken issue as I make sure to send a request again with the given nextPageToken when it exists.
I am running this on a folder that has a little under 4,000 descendant folders in it and a little under 25,000 descendant files in it.
I feel like this must be a bug related to using multiple folders in the q parameter in a single request, considering that I can perform the exact same process and will get different results seemingly randomly.
I suggest you abandon the approach you've taken. Making so many calls to Drive will take forever and possibly give you quota problems.
It's much, much simpler to simply fetch all the folders in a single query, and then build an in-memory hierarchy of the folder ID's you're interested in. Then run a second set of queries to fetch files with those parents.
Alternatively, if these files are being created by an application, make them all children of a common dummy parent folder that you can query against.
I found a similar issue when looking for all files a given user owns, eg:
'example.user#company.com' in owners and trashed=false
I have about 5000 files and usually I can iterate through all of them via pagination. Some days however (like today) I only get <100 results with the query above. When I rewrite my code to fetch files for a given parent-ID and then recursively iterate through the sub-folders, I will get all files. Afterwards the original query succeeds again as well.
It looks like some kind of caching issue on the google-drive server to me.

Checking if a file has been edited

I want to know if there is a way to check if a file has been edited. I looked for methods that can make it for me on Google Apps library, but I have found nothing about it. I don't know if I searched wrong.
Basically, I need to take a file, take a measurable data (something like size) of this file and store on a variable. Then, I need to take that measurable data again, store on another variable, and compare if there was a change. I need a boolean return.
Anyone?
You could do the following (pseudo with links to documentation):
Get the file you want to check using the DocList Class.
Get the ID of that File once you have it using File.getID()
Get the last edit timestamp using File.getLastUpdated()
Store this value in a Spreadsheet, or maybe Script or User Properties.
When you want to check to see if the File was updated, simply File.getFileById()
Repeat step 3.
Then compare the two last-edited timestamps with an operator like !=, or do more complex comparisons on the Dates if you want.
Depending on the result of step 7, return true or false.
Google's documentation is great, for all their services. You just need to read it a bit to understand what kind of power you have through scripting. Hopefully my pseudo-method helps in solving your problem!
Look at the file update date: https://developers.google.com/apps-script/reference/drive/file#getLastUpdated() and for storing data look up the storing data section in the apps script help page.
You could also use the GAT General Audit Tool http://goo.gl/hzZ2yf... which reports when files were edited , viewed and much more.

Google script DocsList service query string not working

I'm making a little script to automatically organize some autogenerated spreadsheets. The goal is to archive the spreadsheets in a directory based on their name (all of them start with the same name but end in a pattern I'm using to organize them). The problem I have is with the function:
lstFile = DocsList.find('type:spreadsheet title:"PROG_GRAL_CENTRE"');
The function doesn't have the query options specified in the docs, but I'm using it on another script and is working fine! I've also tried putting only:
lstFile = DocsList.find('PROG_GRAL_CENTRE');
which should find 200 documents, but none is found! Actually, if I type PROG_GRAL_CENTRE into the search box of my google Drive, all the documents are found, so I don't know what's wrong with my search filter.
Any thoughts?
The method find(query) looks for a string in file content, not only on file name... so even it it worked normally (meaning without the issue you mentioned) I'm not sure it would be a good solution for your use case since there would be a risk that some filename could be found inside another doc with another name (as a reference for example...).
Why don't you try getting all filenames in an array and search into this array instead ?
This can be done very easily and would bring an elegant solution to your problem wouldn't it ?
I'm working on this very same type of script right now ;-) here is the part that gets my files (just an example if ever you are interested):
var doclist=DocsList.getRootFolder().getFilesByType("document",0,2000);
var names = new Array();
for (nn=0;nn<doclist.length;++nn){
if(doclist[nn].getName().match("IMPRESSION_")=="IMPRESSION_"){
names.push([doclist[nn].getName(),doclist[nn].getId()]);
}
}
after that I sort the array the way I want and show the result in a list UI.