简体   繁体   English

如何检查Google云端硬盘中是否存在文件(按名称)?

[英]How do I check if a file exists (by name) in Google Drive?

Using Google Apps Script, I'm trying to check if a file exists in Google Drive, given its name ( nomeDB ). 我正在使用Google Apps脚本检查给定文件名( nomeDB )的文件是否存在于Google云端硬盘中。

Everywhere except in the trash. 除了垃圾桶之外的所有地方。

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

Now, DriveApp.getFilesByName returns a FileIterator, which is an object. 现在, DriveApp.getFilesByName返回一个FileIterator,它是一个对象。 If the object size is zero, or it doesn't exist, this should solve the problem, right? 如果对象大小为零或不存在,这应该可以解决问题,对吗?

Instead of using the .next(), use the .hasNext() function. 代替使用.next(),而使用.hasNext()函数。 I think you would run into problems with .next() trying to grab a file that doesnt exist. 我认为您可能会遇到.next()尝试获取不存在的文件的问题。
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 我不确定100.getFilesByName()方法是否返回空FileIterator或在找不到文件时返回null,但是如果遇到问题,可以通过将if语句条件更改为
(!hasBDs.hasNext() || haBDs == null)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM