如何检查Google云端硬盘中是否存在文件(按名称)? [英] How do I check if a file exists (by name) in Google Drive?

查看:120
本文介绍了如何检查Google云端硬盘中是否存在文件(按名称)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google Apps脚本,我试图检查Google Drive中是否存在一个文件,并指定其名称( nomeDB )。

Using Google Apps Script, I'm trying to check if a file exists in Google Drive, given its name (nomeDB).

垃圾桶除外。

var haBDs = DriveApp.getFilesByName(nomeDB);
  if(!haBDs.next()){
    Logger.log('It does not exist');
  }

现在, DriveApp.getFilesByName 返回一个FileIterator,它是一个对象。如果对象大小为零,或者它不存在,这应该可以解决问题,对吗?

Now, DriveApp.getFilesByName returns a FileIterator, which is an object. If the object size is zero, or it doesn't exist, this should solve the problem, right?

推荐答案

.next()使用.hasNext()函数。我想你会遇到问题.next()试图抓住一个不存在的文件。

除了你的代码看起来很好!

我不是100%确定if当没有找到文件时,.getFilesByName()方法返回一个空FileIterator或null,但如果遇到问题,可以通过将if语句条件更改为

(! hasBDs.hasNext()|| haBDs == null)

Instead of using the .next(), use the .hasNext() function. I think you would run into problems with .next() trying to grab a file that doesnt exist.
Other than that your code looks fine!
I am not 100% sure if the .getFilesByName() method returns an empty FileIterator or null when no files are found, but if you are having issues you could test this by changing your if statement conditions to
(!hasBDs.hasNext() || haBDs == null)

这篇关于如何检查Google云端硬盘中是否存在文件(按名称)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆