How to search for a file in subfolders inside subfolders using apps script?

onit

I'm trying to find a file which may be in a subfolder, but it could also be inside a subfolder of a subfolder. I've followed this answer's approach, but still...

When I place the file inside the 'second subfolder, the script is not finding it.

function updateClientWb() {
  var clientId = 'AA001'
  var parent_folderID = 'folderidsafafdasadasda2D-U';

  var parent_folder = DriveApp.getFolderById(parent_folderID);
  var subFolders = parent_folder.getFolders();
  var searchForClientId = "title contains '" + clientId + "'";
  var clientSeoProjectWb = [];

  while (subFolders.hasNext()) {
    var thisSubFolder = subFolders.next();//THIS IS PROBABLY WHERE THE FLAW IS
    var clientFiles = thisSubFolder.searchFiles(searchForClientId);
    while (clientFiles.hasNext()) {
      var files = clientFiles.next();
      if (files.getName().indexOf('File Name Part') > -1) {
        clientSeoProjectWb.push(files.getId());
      }
    }
  }
  return;
}

Appreciate your help

pgSystemTester

Try something like this. Note that the clientSeoProjectWB is defined in the script file, not a function so both functions can interact with it. I didn't test it, but I think this would put you on the proper track. Make sure your first folder is identified in the startSearch function.

//need to have shared array
var clientSeoProjectWb =[];

function startSearch(){
  searchFolder_(DriveApp.getFolderById('???topFolderID'))
  //Final array with all files...
  clientSeoProjectWb.forEach(x => Logger.log(x));
}

function searchaFolder_(_aFolder) {
  var someFiles = _aFolder.getFiles();

  while(someFiles.hasNext()){
    var aFile = someFiles.next();
    if(aFile.getName().indexOf('File Name Part') > -1){
      //Found file add to array
      clientSeoProjectWb.push(aFile.getId());
    }
  }
  var someFolders = _aFolder.getFolders();
    while(someFolders.hasNext()){
    var newFolder = someFolders.next();
    //launch function again with new folder
    searchaFolder_(newFolder);
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Getting files from subfolders of Dropbox folder using Google Apps Script

Batch-Script: Search subfolders, find file, delete all else

Script to write all filenames inside a folder and its subfolders to a csv file

Using Python search for a string in each file in a folder and subfolders

How to search/delete multiple subfolders?

How can I automate creating a folder, subfolders, and an empty file inside?

How to find all the empty subfolders inside the folder using powershell?

grep -r (or -R) does not search inside subfolders

How to delete all files inside subfolders, but not subfolders itselves?

Executing a command inside folders and subfolders with shell script

using a batch to copy single file into subfolders and their subfolders and so on

How can I scan for a file in subfolders using C#?

How to access Folder/Subfolders/Files using nested loops for file conversions?

How to edit file extensions across multiple subfolders using powershell or other

List the names and Urls of the files from folder and subfolders of dropbox using Google Apps Script

Using Google Apps Script to find Docs/Slides/Sheets in all folders (and subfolders)

In Google Apps Script, how would I get all subfolders and all subsubfolders and all subsubsub folders etc.?

How to exclude subfolders and files in the Search pane of LightTable?

How to fetch assets inside vendor subfolders in Rails?

delete all subfolders using a batch file?

sort subfolders and pngs inside subfolders in python

vifm search files in subfolders

Excel VBA search subfolders

How to find particular file in all subfolders in unix

How to rename multiple file name include subfolders?

how to display all folders,subfolders and file in ckfinder

How to one file copy to all subfolders

How to retrieve file names from subfolders

How can I count subfolders in a Batch file