如何使用应用程序脚本在子文件夹内的子文件夹中搜索文件?
How to search for a file in subfolders inside subfolders using apps script?
我正在尝试查找可能位于子文件夹中的文件,但它也可能位于子文件夹的子文件夹中。
我遵循了 方法,但仍然...
当我将文件放入第二个子文件夹时,脚本找不到它。
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;
}
感谢您的帮助
尝试这样的事情。请注意,clientSeoProjectWB
是在脚本文件中定义的,而不是函数,因此两个函数都可以与之交互。我没有测试它,但我认为这会让你走上正确的轨道。确保您的第一个文件夹在 startSearch
函数中被识别。
//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);
}
}
文件夹中的文件
function getFilesAndFolders() {
const fldr = DriveApp.getFolderById(gobj.globals.projectsfolderid);
SpreadsheetApp.getActive().getSheetByName('Sheet0').clearContents();
SpreadsheetApp.getActive().toast('Entry');
getFnF(fldr);
SpreadsheetApp.getActive().toast('EOF');
}
function getFnF1(folder = DriveApp.getRootFolder()) {
let tree = JSON.parse(PropertiesService.getScriptProperties().getProperty('FnF'));
//Logger.log(JSON.stringify(tree));
if (tree.level < level) {
tree.level = level;
PropertiesService.getScriptProperties().setProperty('FnF', JSON.stringify(tree));
}
const files = folder.getFiles();
let row = Array.from([...Array(level).keys()], ((x, i) => { if (i == level - 1) { x = folder.getName(); } else { x = ''; } return x; }));
tree.txt.push(row);
row = Array.from([...Array(level).keys()], ((x, i) => { if (i == level - 1) { x = 'bold'; } else { x = 'normal'; } return x; }));
tree.fwt.push(row);
PropertiesService.getScriptProperties().setProperty('FnF', JSON.stringify(tree));
if (files.hasNext()) {
let row = Array.from([...Array(level).keys()], ((x, i) => { if (i == level - 1) { x = 'Files:'; } else { x = ''; } return x; }));
tree.txt.push(row);
tree.fwt.push(['normal']);
PropertiesService.getScriptProperties().setProperty('FnF', JSON.stringify(tree));
}
while (files.hasNext()) {
let file = files.next();
let row = Array.from([...Array(level + 1).keys()], ((x, i) => { if (i == level) { x = file.getName(); } else { x = ''; } return x; }));
tree.txt.push(row);
tree.fwt.push(['normal']);
PropertiesService.getScriptProperties().setProperty('FnF', JSON.stringify(tree));
}
const subfolders = folder.getFolders()
while (subfolders.hasNext()) {
let subfolder = subfolders.next();
level++;
getFnF1(subfolder);
}
level--;
}
我正在尝试查找可能位于子文件夹中的文件,但它也可能位于子文件夹的子文件夹中。
我遵循了
当我将文件放入第二个子文件夹时,脚本找不到它。
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;
}
感谢您的帮助
尝试这样的事情。请注意,clientSeoProjectWB
是在脚本文件中定义的,而不是函数,因此两个函数都可以与之交互。我没有测试它,但我认为这会让你走上正确的轨道。确保您的第一个文件夹在 startSearch
函数中被识别。
//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);
}
}
文件夹中的文件
function getFilesAndFolders() {
const fldr = DriveApp.getFolderById(gobj.globals.projectsfolderid);
SpreadsheetApp.getActive().getSheetByName('Sheet0').clearContents();
SpreadsheetApp.getActive().toast('Entry');
getFnF(fldr);
SpreadsheetApp.getActive().toast('EOF');
}
function getFnF1(folder = DriveApp.getRootFolder()) {
let tree = JSON.parse(PropertiesService.getScriptProperties().getProperty('FnF'));
//Logger.log(JSON.stringify(tree));
if (tree.level < level) {
tree.level = level;
PropertiesService.getScriptProperties().setProperty('FnF', JSON.stringify(tree));
}
const files = folder.getFiles();
let row = Array.from([...Array(level).keys()], ((x, i) => { if (i == level - 1) { x = folder.getName(); } else { x = ''; } return x; }));
tree.txt.push(row);
row = Array.from([...Array(level).keys()], ((x, i) => { if (i == level - 1) { x = 'bold'; } else { x = 'normal'; } return x; }));
tree.fwt.push(row);
PropertiesService.getScriptProperties().setProperty('FnF', JSON.stringify(tree));
if (files.hasNext()) {
let row = Array.from([...Array(level).keys()], ((x, i) => { if (i == level - 1) { x = 'Files:'; } else { x = ''; } return x; }));
tree.txt.push(row);
tree.fwt.push(['normal']);
PropertiesService.getScriptProperties().setProperty('FnF', JSON.stringify(tree));
}
while (files.hasNext()) {
let file = files.next();
let row = Array.from([...Array(level + 1).keys()], ((x, i) => { if (i == level) { x = file.getName(); } else { x = ''; } return x; }));
tree.txt.push(row);
tree.fwt.push(['normal']);
PropertiesService.getScriptProperties().setProperty('FnF', JSON.stringify(tree));
}
const subfolders = folder.getFolders()
while (subfolders.hasNext()) {
let subfolder = subfolders.next();
level++;
getFnF1(subfolder);
}
level--;
}