-1

I created 3 untitled Google Spreadsheets to test this script. Search in My Drive for title:untitled or just untitled found all three, but in this script searchFiles method finds the Doc and the Slide but not the Spreadsheet and getFilesByName does not find any! I also tried "untitled" with the getFilesByName and still found none. Any ideas what is wrong?

function findUntitledFiles() {
  Logger.log("Begin findUntitledFiles()");
  var fileName = "*untitled*";
  var file, folder , filesIt , foldersIt;
  filesIt = DriveApp.searchFiles(' title contains "untitled" ') ;  // did not find spreadsheet
//  filesIt = DriveApp.getFilesByName(fileName) ;   // found none
  while (filesIt.hasNext() )  {
    file = filesIt.next() ;
    Logger.log("file.getName() = " + file.getName() );
    foldersIt = file.getParents() ;
    folderName = (foldersIt.hasNext()) ? foldersIt.next().getName() : "unknown" ;
    Logger.log( "folder  " + folderName + "    file name: " + file.getName() );
  } 
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
aNewb
  • 188
  • 1
  • 12
  • As a busy teacher, I tend to leave untitled files everywhere. When students do not complete assignments in classroom untitled files are created for each student. I like to clear up everything once in a while. I do not want an exact match. – aNewb Oct 26 '20 at 23:12

2 Answers2

1

For DriveApp.getFilesByName(fileName):

fileName should contain the actual string of the filename. As far as I know, no wildcard characters are permitted. Also, a slide, doc, and sheet would all have different filenames - Untitled presentation, Untitled document, Untitled spreadsheet. You could search for them this way (if you are only specifically looking at docs, sheets, slides):

function findUntitledFilesByName() {
    var fileCnt = 0;  
    var fileNames = ["Untitled presentation", "Untitled spreadsheet", "Untitled document"];

    for (var fileName in fileNames) {
        var files = DriveApp.getFilesByName(fileNames[fileName]);
        while (files.hasNext()){
            var file = files.next();
            var folders = file.getParents();
            var folderName = (folders.hasNext()) ? folders.next().getName() : "unknown" ;
            console.log( "folder  " + folderName + "    file name: " + file.getName());
            fileCnt++;
        };
    }  
}

For DriveApp.searchFiles(' title contains "untitled" '):

Are you sure your code is not finding the spreadsheet? When I tried your code, it found all the untitled files fine. The only difference is that I have the variable declaration within the while statement so that there are no 'old' values within the variables. Side note... since it uses a parameter of 'title contains', to search for anything that contains the word untitled, it will return results of any filename that has the word 'untitled' in it. So if you have a file "One-Offs Untitled", that will be returned as a search result. Please keep that in mind!

function findUntitledFilesUsingSearchFiles() {
    var fileCnt = 0;
    var files = DriveApp.searchFiles(' title contains "untitled" ') ;

    while (files.hasNext()){
        var file = files.next();
        var folders = file.getParents();
        var folderName = (folders.hasNext()) ? folders.next().getName() : "unknown" ;
        console.log( "folder  " + folderName + "    file name: " + file.getName());
        fileCnt++;      
    };
}
Laura Dye
  • 274
  • 2
  • 6
0

I created a new spreadsheet for which I was the owner and now it is found by the script.

aNewb
  • 188
  • 1
  • 12