If I just box Files.List
in a try-catch
block like I've seen in other answers (like this one), search by name and the file doesn't exist, I just catch an exception with File not found
404 error in the exception message.
I don't think the only way to know if a file exists or not is doing something like Contains("404")
on a catched exception message... Is it?
I've searched on google and here, and I can't find anything apart from "use Files.List()
".
Am I missing something here?
If my understanding is correct, how about this method?
When a file is searched by the filename, you can use search query q
of the files.list method in Drive API. In your case, you can use q=name='fileName'
. When the files.list method is requested using q=name='fileName'
, if the file with the filename of fileName
was not found, the property of files
of the response has the empty array like {"files": []}
. By this, I confirm the existence of the file.
The endpoint and sample script are as follows.
GET https://www.googleapis.com/drive/v3/files?q=name%3D'fileName'
FilesResource.ListRequest listRequest = service.Files.List();
listRequest.Q = "name='fileName'";
var files = listRequest.Execute();
trashed=false
to q
like name='fileName' and trashed=false
.If this was not what you want, I apologize.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With